首页 > 其他 > 详细

多态中的转型

时间:2016-07-24 16:03:22      阅读:132      评论:0      收藏:0      [点我收藏+]

public class PolDemo01 {
public static void main(String[] args) {
A a = new B();
a.tell1(); //B--tell1  子类重写了父类的方法
a.tell2(); //A--tell2  父类用子类实例化,调用自己的方法

B b = (B) a;
b.tell1(); //B--tell1  调用子类自己的方法
b.tell2(); //A--tell2  子类继承父类,调用父类的方法
b.tell3(); //B--tell3  调用子类自己的方法
}
}


class A {
public void tell1() {
System.out.println("A--tell1");
}

public void tell2() {
System.out.println("A--tell2");
}
}


class B extends A {
public void tell1() {
System.out.println("B--tell1");
}

public void tell3() {
System.out.println("B--tell3");
}
}

多态中的转型

原文:http://www.cnblogs.com/984187775blog/p/5700779.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!