Detail
public class Detail {
String address="AAAAABBBBB";@Override
public String toString() {
return "address:" +address;
}
}
Infor
public class Infor {
double height=160;double weight=55;
boolean gender=true; // true为男性 false为女性
Detail detail;
public Infor() {
detail = new Detail();
}
// @Override
// public String toString() {
// String string = "weight:" +weight+
// "height:" +height+
// "gender:" +(gender?"男":"女")+
// detail.toString();
// return string;
// }
}
Student
int age;
String name;
String id;
Infor infor;
int[] scoreArr;
public Student() {
//若有 自定义类型(引用类型)的成员
// 在构造方法中 开辟实际空间
infor = new Infor();
scoreArr = new int[3];
}
void disp(){
System.out.println(
"name:"+this.name +
" id:"+this.id+
" age:"+this.age+
" height:"+this.infor.height
);
}
public boolean equals(Student arg0) {
if (this.age == arg0.age &&
this.id.equals(arg0.id)) {
return true;
}
return false;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + ", id=" + id + ", infor=" + infor + ", scoreArr="
+ Arrays.toString(scoreArr) + "]";
}
// @Override
// public String toString() {
// String string = "name:"+this.name +
// " id:"+this.id+
// " age:"+this.age+" "+
// infor.toString();
//
// return string;
// }
}
test
public class Test {
public static void main(String[] args) {
/*----
* !!! 这不是继承 是组成
* Student 类型
* age
* name
* String id
* infor
*
* void disp()
*
* Infor 类型
* boolean gender
* height
* weight
* Detail detail
*
* Detail 类型
* address
*
* -----*/
Student stu = new Student();
//若自定类型的某个属性 是另一个自定义类型的引用
// 另一个自定义类型的引用 必须被赋予一个实际的对象
// 下面方法可以 但不推荐 建议修改构造函数
// stu.infor = new Infor();
System.out.println( stu.scoreArr);
System.out.println(stu.infor.detail.address);
// stu.disp();
System.out.println(stu);
//-----如何判定两个引用类型是否相同
// 如何判定引用的 实例中数据是否相同
// 重写equals()方法 自行定义规则
//-----
Student stu2 = new Student();
stu.age = 10;
stu2.age = 10;
stu.name = "Tom";
stu2.name = "Tom";
if (stu2.equals(stu)) {
System.out.println("!!!");
}
}
}