日期:2014-05-20  浏览次数:20693 次

想调用被覆盖的父类方法,怎么写?

class   Test
{
public   static   void   main(String   []   args)
{
chl   one   =new   chl();
one.show();                   //若在这想调用   one   父类的   show()   怎么写?
}
}

class   far
{
void   show()
{
System.out.println( "far ");
}
}

class   chl   extends   far
{
void   show()
{
System.out.println( "chl ");
}
}

------解决方案--------------------
super.xxx()
------解决方案--------------------
那你要在子类中加入要调用父类的代码..
class chl extends far
{
void show()
{
super.show();
System.out.println( "chl ");
}
}
这样就调用了一个被你覆盖的父类的方法.
然后实例子类对象..就可以使用子类的这个方法调用了.
------解决方案--------------------
那肯定不行.
是在类中完成的..然后才实例对象.
------解决方案--------------------
one.super.show()
这种写法头一次见,真有创意,^_^
在子类的show()方法调用基类的就可以了!
------解决方案--------------------
class chl extends far
{
void show()
{
System.out.println( "chl ");
}

void showSuper(){
super.show();
}
}

另外定义一个函数吧,one.showSuper();