问题1:外部类能不能访问内部类?(域也好方法也好,通过内部类对象来访问不算)
public class TestInner { class Inner{
public int a = 10;
public int get(){
return a;
}
}
public static void main(String[] args){
TestInner testInner = new TestInner();
//这段代码,里的内部类能在不创建内部类对象的情况下被访问?
}
}问题2:有段代码的解释不明白。
class Parcel4 {
private class PContents implements Contents {
private int i = 11;
public int value() { return i; }
}
protected class PDestination implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
}
public Destination destination(String s) {
return new PDestination(s);
}
public Contents contents() {
return new PContents();
}
}public class TestParcel {
public static void main(String[] args) {
Parcel4 p = new Parcel4();
Contents c = p.contents();
Destination d = p.destination("Tasmania");
}
}
Parcel4中增加了一些新东西:内部类PContents是private,所以除了Parcel4,没有人能访问它。PDestination是protected,所以只有Parcel4及其子类、还有与Parcel4同一个包中的类能访问PDestination,其他类都不能访问PDestination。这意味着,如果客户端程序员想了解或访问这些成员,那是要受到限制的。实际上,甚至不能向下转型成private内部类(或protected内部类,除非是继承自它的子类),因为不能访问其名字,就像在TestParcel类中看到的那样。问题3:有谁知道Array类是放在java.lang.reflect包中的,它和映射有什么关系?
public class TestInner { class Inner{
public int a = 10;
public int get(){
return a;
}
}
public static void main(String[] args){
TestInner testInner = new TestInner();
//这段代码,里的内部类能在不创建内部类对象的情况下被访问?
}
}问题2:有段代码的解释不明白。
class Parcel4 {
private class PContents implements Contents {
private int i = 11;
public int value() { return i; }
}
protected class PDestination implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
}
public Destination destination(String s) {
return new PDestination(s);
}
public Contents contents() {
return new PContents();
}
}public class TestParcel {
public static void main(String[] args) {
Parcel4 p = new Parcel4();
Contents c = p.contents();
Destination d = p.destination("Tasmania");
}
}
Parcel4中增加了一些新东西:内部类PContents是private,所以除了Parcel4,没有人能访问它。PDestination是protected,所以只有Parcel4及其子类、还有与Parcel4同一个包中的类能访问PDestination,其他类都不能访问PDestination。这意味着,如果客户端程序员想了解或访问这些成员,那是要受到限制的。实际上,甚至不能向下转型成private内部类(或protected内部类,除非是继承自它的子类),因为不能访问其名字,就像在TestParcel类中看到的那样。问题3:有谁知道Array类是放在java.lang.reflect包中的,它和映射有什么关系?
甚至不能向下转型成private内部类继承本来只能扩大的,不能收缩的,父类接口已经约定承诺了public方法,子类的实现变成private,相当于毁约了,子类就没有实现嘛,呵呵
提供了一些静态的公共方法,访问array数组类型
看api
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/reflect/Array.html
内部类其实理解为成员变量,就好了!TestInner a = new TestInner();
a.new Inner().get();有三种内部类!呵呵
2.不懂
3.Array与java.lang.reflect有什么关系啊,学习
引用,但没听过外围类拥有内部类的一个隐式引用,下面为test
package Test;
public class Test1 { class Inner{
public int a = 10;
public int get(){
return a;
}
}
public void f()
{
//System.out.println(a); //a cannot be resolved
}
//那么换个方法,把a定义为static,那么此内部类也要定义为static
static class Inner1{
public static int a=11;
public int get(){
return a;
}
}
public void g()
{
//System.out.println(a);//a cannot be resolved
System.out.println(Inner1.a);//true 但是毫无意义
}
public static void main(String[] args){
Test1 testInner = new Test1();
//这段代码,里的内部类能在不创建内部类对象的情况下被访问?
}
}