test

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("!!!");
}

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值