public class Base
{
private int a;
public Base(int a)
{
this.a=a;
}
public int getA()
{
return a;
}
public class Sub extends Base
{
private int b;
public Base(int a,int b)
{
super(a);
this.b=b;
}
public int getB()
{
return b;
}
}
public void main(String[] args)
{
Sub sub=new Sub(1,2);
System.out.println("a="+sub.getA()+"b="+sub.getB());
}
}
------解决方案--------------------
想问一下你是要练习内部类吗?如果不是要这样才会有结果
public class Base {
private int a;
public Base(int a) {
this.a = a;
}
public int getA() {
return a;
}
public static void main(String[] args) {
Sub sub = new Sub(1, 2);
System.out.println("a=" + sub.getA() + "b=" + sub.getB());
}
}
class Sub extends Base {
private int b;
public Sub(int a, int b) {
super(a);
this.b = b;
}
public int getB() {
return b;
}
}
------解决方案--------------------
把a,b定义成public就好了,在类b中不能直接引用a中的私有成员变量。。。。。
------解决方案--------------------
当需要创建内部类对象,例如你上面的例子你就需要 在主函数中写如下[code=java][Base b=new Base(1);
Base.Sub sub=b.new Sub(1,2);/code]
------解决方案--------------------
Base b=new Base(1);
Base.Sub sub=b.new Sub(1,2);
------解决方案--------------------
你如果将一个类A放在另一个类B的内部去定义 那么类A就是成员内部类 那么你在main中实例化A的时候 就需要通过外部类来实例化内部类 如回帖5 可是如果你将子类放在外部 他就是一个正常的类 可以按正常的方法实例化就如回帖1
------解决方案--------------------

------解决方案--------------------

------解决方案--------------------
main函数是程序的入口 我要是将sub放在base外部的话,我就直接可以用sub了,可是如果我将sub放在base内部的话我就需要通过base访问到sub
------解决方案--------------------
public class Base {
private int a;
public Base(int a) {
this.a = a;
}
public int getA() {
return a;
}
class Sub extends Base {
private int b;
public Sub(int a, int b) {
super(a);
this.b = b;
}
public int getB() {
return b;
}
}
public static void main(String[] args) {