//通过super访问父类成员,为什么会出错 ?
public class Super {
int a=1;
public class subclass extends Super{
int a=6;
int b=super.a;
public void main(String[] args){
int a=6;
subclass p=new subclass();
System.out.println(a+"\n");
System.out.println(p.a);
System.out.println(p.b);
}
}
}
[此贴子已经被作者于2007-11-18 10:27:18编辑过]
----------------解决方案--------------------------------------------------------
有super.a吗?
应该先实例一个对象,再调用。
----------------解决方案--------------------------------------------------------
还有,静态方法是不能访问类的非静态成员的。这里的内部类是做为外部类的成员变量的。要想在main()中访问,需把内部类定义为static的。
class Super {
int a=1;
public static class SubClass extends Super{
int a=6;
int b=super.a;
}
public static void main(String[] args){
int a=6;
SubClass p=new SubClass();
System.out.println(a+"\n");
System.out.println(p.a);
System.out.println(p.b);
}
}
----------------解决方案--------------------------------------------------------
谢谢哦,运行一下吧,还是不对。
不过将Super类和其他分开是可以的。
???
----------------解决方案--------------------------------------------------------
----------------解决方案--------------------------------------------------------
[CODE]
class Super {
int a=1;
public class SubClass extends Super{
int m=6;
int b=a;
}
public static void main(String[] args){
int a=6;
Super.SubClass p=new Super().new SubClass();
System.out.println(a+"\n");
System.out.println(p.m);
System.out.println(p.b);
}
}
类部类的使用!
----------------解决方案--------------------------------------------------------
明白了!谢谢各位!呵呵~~
----------------解决方案--------------------------------------------------------