父类的引用指向子类的对象,它只能看的到父类的那些方法~ 子类自身的方法看不到~~
·······························
如:
interface Singer { //定义了一个接口,它属于特殊的抽象类,
方法不用去实现,叫它的子类去实现它的方法
Public void sing();
public void sleep();
}
class Student implements Singer {
private String name;
Student(String name) {
this.name = name;
}
public void study {
System.out.println("studying ,,,,");
}
public String getName() {
return name;
}
public void sing() {
System.out.println("student is sing");
}
public void sleep() {
System.out.println("student is sleep");
}
}
public class Test {
public static void main(String[] args) {
Singer s1 = new Student("le"); //父类的引用指向子类的对象
s1.sing(); s1.sleep();
}
}
······························
打印结果为:
student is singing (分析:因为new的s1这个对象只能看到子类中 父类的方法,子类的方法是看不到的)
即父类的引用指向子类的对象
student is sleeping