
概念:如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类。
例如:假设有两种具体的水果(苹果(Apple类)和香蕉(Banana类)且都继承水果(Fruits)这个类,在水果类中有一个colour的方法,我们都知道子类继承父类需要重写子类中的方法,但是不同的水果的颜色不同,所以水果类中colour方法无法具体实现,但是苹果和香蕉这两个类是一种具体的水果,所以它们的colour方法能够具体实现),因此我们就可以把水果(fruits类)设计为**“抽象类”**。
我们可以发现在上图中国Frutis类中的colour()方法中并没有什么实际的行动内容,在java中我们可以把这一类方法设置为抽象方法,包含抽象方法的类我们叫做“抽象类”。
Java中被abstract修饰的类叫抽象类,被abstract修饰的方法叫抽象方法且抽象方法且不用给出具体的实现体。
// 抽象类即被abstract修饰的类
public abstract class Fruits {
// 抽象方法即被abstract修饰的方法,不用给出具体的方法体
abstract public void colour(); //颜色
abstract void shape();//形状
注意:抽象类也是类,和普通的类一样内部可以有普通方法和属性以及构造方法,但是不能进行实例化。
接口是一种行为的规范和标准,Java中可以把接口看成多个类的公共规范,是一种引用新型数据类型。
接口的语法和定义类的语法基本相同,只需将定义类中的关键字class改成关键字interface,即定义了一个接口。
//用关键字interface定义一个接口
public interface 接口名称{
// 抽象方法
public abstract void method1(); // public abstract 是固定搭配,可以不写
void method2();
}
Java中类和类之间只能为单继承,但是一个类却可以实现多个接口,并且接口和接口之间也可以多继承,因此解决多继承问题。
核心区别
object类是Java中默认的一个类,除了object类,Java中所有的类都是继承这个类的,也就是说Object类是Java中所有类的父类,Java中所有类的对象都可以使用object引用接收。
Java中的很有用的一个接口——Cloneable(是一个浅拷贝),Object类中存在一个clone()的方法,这个方法在调用时必须先实现Cloneable接口, 才能被合法调用 , 否则就会抛出 CloneNotSupportedException 异常,这个方法的调用可以创建一个对象的 “拷贝”,从而实现浅拷贝。
class Fraction{
public double a = 99;
}
// 实现Cloneable接口并重写clone()方法
class Student implements Cloneable{
public Fraction fraction = new Fraction();
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Demo1 {
public static void main(String[] args) throws CloneNotSupportedException {
Student student1 = new Student();
Student student2 = (Student) student1.clone(); //用clone()方法创建一个对象的拷贝
System.out.println("student1修改之前的分数");
System.out.println(student1.fraction.a);
System.out.println(student2.fraction.a);
student2.fraction.a = 59;
System.out.println("student1修改后的分数");
System.out.println(student1.fraction.a);
System.out.println(student2.fraction.a);
}
}
运行结果可知
在认识了浅拷贝之后我们就要思考什么是深拷贝呢?
深拷贝不是某个方法是深拷贝,而是在代码的层次上进行的,需要从代码的实现上来看待的,意思就是若想达到深拷贝,那么在每个对象中,如果有引用这个对象的引用所指向的其他对象,那么这个所指向的其他对象也要进行拷贝。
class Fraction implements Cloneable{ //由深拷贝可知这个对象也需要进行clone拷贝,所以也需要实现Cloneable接口
public double a = 99;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Student implements Cloneable{
public Fraction fraction = new Fraction();
@Override
protected Object clone() throws CloneNotSupportedException {
// return super.clone();
Student student = (Student) super.clone();
student.fraction = (Fraction) this.fraction.clone(); //每个对象的引用所指向的其他对象的也需要进行拷贝
return student;
}
}
public class Demo1 {
public static void main(String[] args) throws CloneNotSupportedException {
Student student1 = new Student();
Student student2 = (Student)student1.clone();
System.out.println("student1修改之前的分数");
System.out.println(student1.fraction.a);
System.out.println(student2.fraction.a);
student2.fraction.a = 59;
System.out.println("student1修改后的分数");
System.out.println(student1.fraction.a);
System.out.println(student2.fraction.a);
}
}
运行结果可知
以上就是今天所学习的内容,主要学习了抽象类和接口的内容,认识到了抽象类和接口的区别,并且运用接口能够解决多继承的问题,也了解到了浅拷贝和深拷贝的区别,还能够使用代码理解这些内容,但掌握的还很初步,希望今后能够多多运用。