- Java code
package p1;public interface SayMe { void say();}
- Java code
package p2;import p1.SayMe;public class Person { protected class PersonSayMe implements SayMe{ public void say(){ System.out.println("PersonSayMe"); } } protected void walk(){ System.out.println("Person walk"); }}
- Java code
package p3;import p2.Person;import p1.SayMe;public class FeMale extends Person{ public SayMe getSayMe(){ return new PersonSayMe(); //为什么这个不能继承Person 的 PersonSayMe ???? } public static void main(String[] args){ new FeMale().walk(); }}
------解决方案--------------------
因为你的PersonSayMe是一个内部类,并且不是static的,其相当于Person 的字段一样,怎么才能使用字段,先有Person 才行
这样该就可以了
- Java code
public class FeMale extends Person{ public SayMe getSayMe(){ return new FeMale().new PersonSayMe(); //为什么这个不能继承Person 的 PersonSayMe ???? } public static void main(String[] args){ new FeMale().walk(); }}