package p54_p60.f222_230.ch02;
class p0{}
class person{ //父类
int a;
//person(){}
}
class student extends person{//子类
//student(){}
int x;
}
public class testArray {
p0 p00;
static person ps[]=new person[3];
student st1,st2,st3;
public static void main(String args[]){
testArray tt=new testArray();
tt.st1=new student();
tt.st2=new student();
tt.st3=new student();
ps[0]=tt.st1;
ps[0]=new person();
ps[1]=tt.st2;
ps[2]=tt.st3;
// ps[0]=tt.p00=new p0(); //类型不匹配 不兼容 不能存到person数组中
}
}
对于数据类型为基本类型,操作代码同C语言,対于类型为抽象数据类型/引用类型,情况略复杂一些。一般定义一个引用类型数组,那么该类型的对象及其子类型的对象,均可以存储于该数组,并可借此机会实现多态、动态绑定,从而设计出更加灵活的程序。
页面更新:2024-05-20
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号