我是编程的新手,所以我只是尝试一些简单的程序...
public class simple {
public static void main(String[] args) {
A ob=new A(10,20);
System.out.println("values of a and b are "+ob.a+" "+ob.b);
} ^
} ^
public class A {
int a;
private int b;
A(){}
A(int c,int d)
{
a=c;
b=d;
}
}
显示的错误是field ob.b not visible
..我正在使用eclipse,需要知道上面的代码有什么问题。
谢谢!
如果您注意到变量b被声明为私有,那么将不允许简单类直接访问它。相反,您可以通过类似getb()的方法来访问它
public class A {
int a;
private int b;
A(){}
A(int c,int d)
{
a=c;
b=d;
}
int getb()
{
return b;
}
}
现在,您可以按以下方式重写打印语句,
System.out.println("values of a and b are "+ob.a+" "+ob.getb());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句