fft123() :如果对于下面这个例子,我要如何才能访问b呢? class A { public class B { public int b; }} xwlovesh(xiewei) :你说的我还是不太明白,既然“内部类只有包含他的外部类能访问.” 怎么又“声明为public,所有类都可以访问。”呢?能不能拿上面那个例子作个说明。
一般Public的内部类,但会声明为static的 这样,外面可以用 new classOut.classIn()来使用
把内部类当成是外部类的一个特殊变量,比如String类型不就可以很好理解了 class A{ public String s; }不就可以A.s了,这里s也正好是个类还可以A.s.... 如果为private时,s也是不能在外部访问的。
class A { public class B { public int b; }} A.B.b=3; 如果这样: class A { private class B//这里改成private了 { public int b; }} 再在class A的外面这样调用:A.B.b=3; 就会报错;但在A的内部仍然可以这样调用
但我在程序中使用A.B.b=3;则会抱错为: w1.java:13: non-static variable b cannot be referenced from a static context A.B.b=6;或者:A a1=new A(); a1.B.b=6; 则会抱错为w1.java:13: unexpected type required: class, package found : variable a1.B.b=6; ^ w1.java:13: non-static variable b cannot be referenced from a static context a1.B.b=6; ^ 请指教
但我在程序中使用A.B.b=3;则会抱错为: w1.java:13: non-static variable b cannot be referenced from a static context A.B.b=6;这是你在main函数(是static的)中调用了非静态的变量A a1=new A(); a1.B.b=6; 则会抱错为w1.java:13: unexpected type required: class, package found : variable a1.B.b=6; 这个是你的class B没有实例,你可以把class B定义成static的或生成B的实例(如b1) 然后a1.b1.b=3;
class A { class B { public int i; } } public class test { public static void main(String[] args) { A a = new A(); A.B b = a.new B(); b.i = 3; System.out.println(b.i); } }写了个测试的类 你自己看吧
前面说错了 b实例化后是个新的对象 不能用a.b这样调用,要直接调用
下面的方法可以访问: public class A{ public class B{ public int c; } public B getB(){ return new B(); } public static void main(String[] args){ A a=new A(); A.B b=a.getB(); b.c=6; } }
声明为public后,就可以用“外部类.内部类”访问这个内部类
声明为private后就只能在包含它的外部类访问了,相当与这个外部类的一个变量
声明为public,所有类都可以访问。
声明为private,只有本类可以可以访问,其他任何类都无法访问。
class A
{
public class B
{
public int b;
}}
xwlovesh(xiewei) :你说的我还是不太明白,既然“内部类只有包含他的外部类能访问.”
怎么又“声明为public,所有类都可以访问。”呢?能不能拿上面那个例子作个说明。
这样,外面可以用
new classOut.classIn()来使用
private内部类为私有内部类,只能被包含它的外部类访问。
class A{
public String s;
}不就可以A.s了,这里s也正好是个类还可以A.s....
如果为private时,s也是不能在外部访问的。
{
public class B
{
public int b;
}}
A.B.b=3;
如果这样:
class A
{
private class B//这里改成private了
{
public int b;
}}
再在class A的外面这样调用:A.B.b=3;
就会报错;但在A的内部仍然可以这样调用
w1.java:13: non-static variable b cannot be referenced from a static context
A.B.b=6;或者:A a1=new A();
a1.B.b=6;
则会抱错为w1.java:13: unexpected type
required: class, package
found : variable
a1.B.b=6;
^
w1.java:13: non-static variable b cannot be referenced from a static context
a1.B.b=6;
^
请指教
w1.java:13: non-static variable b cannot be referenced from a static context
A.B.b=6;这是你在main函数(是static的)中调用了非静态的变量A a1=new A();
a1.B.b=6;
则会抱错为w1.java:13: unexpected type
required: class, package
found : variable
a1.B.b=6;
这个是你的class B没有实例,你可以把class B定义成static的或生成B的实例(如b1)
然后a1.b1.b=3;
fft123() :当B类声明为public后,我要如何才能引用B呢?如我怎么才能定义B的实例呢?A.B b1=new A.B();这句通不过编译呀。直接B b1=new B();也不行。
{
class B
{
public int i;
}
}
public class test
{
public static void main(String[] args)
{
A a = new A();
A.B b = a.new B();
b.i = 3;
System.out.println(b.i);
}
}写了个测试的类
你自己看吧
b实例化后是个新的对象
不能用a.b这样调用,要直接调用
public class A{
public class B{
public int c;
} public B getB(){
return new B();
} public static void main(String[] args){
A a=new A();
A.B b=a.getB();
b.c=6;
}
}
protected:是保护,可以被自己和自己的子类访问
public:是公有的可以被任何引用访问,建议不要使用直接的公用域