首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

求详解,运作不出结果

2013-05-02 
求详解,运行不出结果public classBase{private int apublic Base(int a){this.aa}public int getA(){re

求详解,运行不出结果
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());
}
}

这个有什么不理解的吗? 正常的类不都是这样定义的吗?你有什么想法?
[解决办法]
求详解,运作不出结果
[解决办法]
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) {


Base b = new Base(1);
Base.Sub sub = b.new Sub(3, 2);
System.out.println("a=" + sub.getA() + "b=" + sub.getB());
}

}

这是把sub放在里面的注意一下主函数  你就会发现发在里面与放在外面的区别
[解决办法]
求详解,运作不出结果
[解决办法]
求详解,运作不出结果
[解决办法]
求详解,运作不出结果
[解决办法]
求详解,运作不出结果姐姐我只有QQ可是基本上不上线
[解决办法]
你把你的main方法放到sub类里就可以了。。。不要单独建个类来测试程序。你的b是private类型的,它的范围只是在sub类里。
[解决办法]
QQ1216164960 等姐姐心情好了就会上线看看 不过真不怎么上线求详解,运作不出结果
[解决办法]
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 Sub(int a, int b) {
super(a);
this.b = b;
}

public int getB() {
return b;
}
}

public static void main(String[] args) {
Sub sub = new Base(1).new Sub(1, 2);
System.out.println("a=" + sub.getA() + "b=" + sub.getB());
}
}

这个内部类要这样实例化,编译应该都错误提示的,要注意看才能提高!

热点排行
Bad Request.