多态

来源:互联网 发布:淘宝卖家正常发货时间 编辑:程序博客网 时间:2024/06/05 11:48
class Father{
int age=59;//成员变量并初始化
public void speak(){
System.out.println("father speak()");
}
public static void method(){//静态方法
System.out.println("father methpd()");
}
}
class Son extends Father{
int age=22;
public void speak(){
System.out.println("son speak()");
}
public void talk(){
System.out.println("son talk()");
}
public static void method(){
System.out.println("son methpd()");
}
}
class PolymTest{
public static void main(String[] args){
Father f=new Father();//没有体现多态,只是通过父类引用调用父类方法
f.speak();
Father f2=new Son();//父类引用指向子类对象,体现多态
f2.speak();//调用方法,如果子类重写了该方法,则调用子类的
System.out.println(f2.age);//59~成员变量没有多态性
f2.method();//father method()~静态方法没有多态性
// f2.talk();//不能调用子类特有的方法, erry
Son s=new Son();
s.speak();
s.talk();
}
}
0 0
原创粉丝点击