预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共108页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

第七章对象群体的组织目录7.1对象数组对象数组 数组元素是类的对象 所有元素具有相同的类型 每个元素都是一个对象的引用静态初始化:在声明和定义数组的同时对数组元素进行初始化,例如: BankAccount[]accounts={ newBankAccount(“Zhang",100.00), newBankAccount(“Li",2380.00), newBankAccount(“Wang",500.00), newBankAccount(“Liu",175.56), newBankAccount(“Ma",924.02)}; 动态初始化:使用运算符new,需要经过两步: 首先给数组分配空间 typearrayName[]=newtype[arraySize]; 然后给每一个数组元素分配空间 arrayName[0]=newtype(paramList); … arrayName[arraySize-1]=newtype(paramList); 使用数组存储一个班的学生信息及考试成绩。学生信息包括学号、姓名、三门课(英语、数学、计算机)的成绩及总成绩。 首先声明学生类Student 属性包括 学号(id),姓名(name),英语成绩(eng),数学成绩(math),计算机成绩(comp),总成绩(sum) 方法包括 构造方法,get方法,set方法,toString方法,equals方法,compare方法(比较两个学生的总成绩,结果分大于,小于,等于),sum方法(计算总成绩) 实现Serializable接口,以便对象持久化,importjava.io.*; publicclassStudentimplementsSerializable{ privateStringid; //学号 privateStringname; //姓名 privateinteng; //英语成绩 privateintmath; //数学成绩 privateintcomp;//计算机成绩 privateintsum; //总成绩 publicStudent(Stringid,Stringname,inteng,intmath,intcomp){ this.id=id; this.name=name; this.eng=eng; this.math=math; this.comp=comp; sum();//计算总成绩 } publicStudent(Students){ this.id=s.id; this.name=newString(s.name); this.eng=s.eng; this.math=s.math; this.comp=s.comp; sum();//计算总成绩 } publicvoidsetId(Stringid){ this.id=id; } publicvoidsetName(Stringname){ this.name=name; } publicvoidsetEng(inteng){ this.eng=eng; sum();//计算总成绩 } publicvoidsetMath(intmath){ this.math=math; sum();//计算总成绩 } publicvoidsetComp(intcomp){ this.comp=comp; sum();//计算总成绩 } publicStringgetId(){ returnid; } publicStringgetName(){ returnname; } publicintgetEng(){ returneng; } publicintgetMath(){ returnmath; } publicintgetComp(){ returncomp; } publicintgetSum(){ returnsum; } voidsum(){ this.sum=eng+math+comp; } publicStringtoString(){ returngetId()+"\t"+getName()+"\t"+getEng()+ "\t"+getMath()+"\t"+getComp()+"\t"+getSum(); } publicbooleanequals(Objectx){ if(this.getClass()!=x.getClass())returnfalse; Studentb=(Student)x; return(this.get