码迷,mamicode.com
首页 > 其他好文 > 详细

多态中的转型

时间:2016-07-24 16:05:47      阅读:88      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!