interface A
{
void run();
}class B
{
public void run()
{
System.out.println("B is Run!");
}
}class C extends B implements A
{
         public void run()
         {
         System.out.println("C is Run!!!");
         }
public A getA()
{
return new C();
}
}class Temp
{
public static void main(String[] args)
{
C c=new C();
A a=c.getA();
c.run();
a.run();
}
}
怎么改上面的代码,才能打印出两条run方法的语句。

解决方案 »

  1.   

    改成能够打印出B和C类中的run的语句
      

  2.   

    可以将B中的构造方法名字改为public void runB(),然后在main中调c.runbB();就可以了。
    public class Test1 {
    public static void main(String[] args) {

    C c=new C();
    A a=c.getA();
    c.runB();
    a.run();
    } }
    interface A {
    public void run();
    }class B {
    public void runB() {
    System.out.println("B is Run!");
    }
    }class C extends B implements A { 
    public void run() {
    System.out.println("C is Run!!!");
    }
    public A getA() {
    return new C();
    }
    }
      

  3.   

    class C 的run()里面加一条
    super.run();
      

  4.   

    我忘记super了,五楼的提醒了,谢谢!
      

  5.   

    C c=new C() 改为 C c = new B()  不知道行不行