public class Adventure extends Class1
implements Interface1,Interface2,Interface3{
public void fight1(){}
public void fight2(){}
public void fight3(){}
public static void main(String[] args){
Class1 a = new Adventure();
a.fight1();
}
}
interface Interface1{
void fight1();
}
interface Interface2{
void fight2();
}
interface Interface3{
void fight3();
}
class Class1{
void fight1(){System.out.println("fsadf");}
}
为什么打印为空,而不是fsadf

解决方案 »

  1.   

    类Adventure中的public void fight1(){}方法把类Class1中的void fight1(){System.out.println("fsadf");} 方法给覆盖了,这就是java中的多态,这里是通过向上转型---即:Class1 a = new Adventure();来完成的。
      

  2.   

    因为在Adventure类里面重写了这个方法,public void fight1(){} ,没有打印任何东西
      

  3.   

    方法fight1()在父类Class1与子类Adventure中都有定义,这时候只会执行子类的方法,也就是执行"public void fight(){}"。
    另外:从你的代码来看,类Adventure中的fight1()是对接口Interface中的方法fight1()的实现,正规来说父类Class1最好也实现Interface(为可读性考虑)。