public class TestA {
class A{
public long a;
}
class B extends A{
public long b;
}
void seta(A data[],int i){
data[i].a = 2;
}
public static void main(String args[]){
TestA aTestA = new TestA();
B data[] = new B[4]; //对象数组
B b1 = aTestA.new B(); //声明一个内部类对象
B b2 = aTestA.new B();
B b3 = aTestA.new B();
B b4 = aTestA.new B();
data[0] = b1;
data[1] = b2;
data[2] = b3;
data[3] = b4;
for(int i =0;i<4;i++){
data[i].a = 1;
data[i].b = 1;
aTestA.seta(data,i); //继承中的向上转型,父类引用指向子类对象
}
for(int i=0;i<4;++i){
System.out.println(data[i].a+"and"+data[i].b);
}
}
}
java中的内部类
最新推荐文章于 2022-04-16 14:09:15 发布