马克社区吧 关注:483贴子:445
  • 0回复贴,共1

java中如何使用派生类指针指向基类,即downcast向下转型?举例!

只看楼主收藏回复

基类指针指向派生类,我们已经很熟了。假如我们想用派生类反过来指向基类,就需要有两个要求:1)马克-to-win:基类指针开始时指向派生类,2)我们还需要清清楚楚的转型一下。


if you want to use derived class pointer
point to base class, there are two requirements:
1) base class pointer is initially the type of the derived class like Animal a2 = new Dog();
2) 马克-to-win:we still need to explicitly cast it like Dog d = (Dog)a2;。。。。。。。。。。
详情请见:http://www.mark-to-win.com/JavaBeginner/JavaBeginner3_web.html#HowToUseDowncasting