extends 和 implements的作用不需要过多解释,
只要记住父类引用指向子类对象时是向上转型,父类引用不可以访问子类新增加的成员,但是今天重写toString()方法发现一些问题
父类:
public interface People {
void save();
}
子类:
public class Student implements People {
String name = "cl";
int age = 24;
@Override
public String toString() {
return "student [name=" + name + ", age=" + age + "]";
}
@Override
public void save() {
}
}
main:
public static void main(String[] args) {
People p = new Student();
System.out.println(p);
}
输出结果:student [name=cl, age=24]
明明是输出的子类的成员变量,但是是子类调用的,查阅了相关资料, 找了好久
参考:java语言规范 9.2 Interface Members
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.
如果一个接口没有直接的父接口,那么,这个接口就隐含的声明了这样的一些成员方法m,这个方法是public abstract的,签名是s,返回类型是r,抛出t类型异常,与Object类中声明的public的,签名是s,返回类型是r,抛出t类型异常的方法相对应,除非,接口中明确定义了这样的方法。
如果接口明确的声明了这样的方法m,m在Object类中是final的,这将会产生编译时error。
接口隐含定义了一套与Object类中的方法签名完全相同的方法,所以,我们在程序中调用接口的那些与Object中具有相同签名的方法时,编译器不会报错。toString()就是这样的一个方法。