1 对象中的对象
**(自我理解)**不是一个真正概念。
可以理解为:在对象中拥有属性,其属性类型是其他类。
public class Student {
public String name;
public String sex;
public int age;
public String stuNo;//学费
}
public class Test {
public static void main(String[] args) {
//实列化一个学生:张大朗
Student stu1 = new Student();
stu1.name = "张大朗";
stu1.age = 17;
stu1.sex = "男";
stu1.stuNo = "80";
}
}
2 对象数组:
数组的类型是某个类。数组中的值是对象。
如果直接输出这个的话为位置。 列如(Student@15db9742)
public class Student {
public String name;
public String sex;
public int age;
public String stuNo;//学费
//引用教师类
public Teacher1 teacher;
public void toStirng(){
System.out.println("姓名"+name+"\t性别"+sex+"\t年龄"+age+"\t学分"+stuNo);
}
}
public class Teacher1 {
public String name;
public String sex;
public int age;
public int workYear;//工作年限
public String major;//专业
//对象数组
public Student[] students = new Student[3];
public String toString(){
return "教师姓名:"+name+"\t性别"+sex+"\t年龄"+age+"\t工作年限:"+workYear+"\t专业"+major;
}
}
public class Test {
public static void main(String[] args) {
//实列化一个学生:张大朗
Student stu1 = new Student();
stu1.name = "张大朗";
stu1.age = 17;
stu1.sex = "男";
stu1.stuNo = "80";
//实列化一个学生:张大朗
Student stu2 = new Student();
stu2.name = "瓜娃子";
stu2.age = 33;
stu2.sex = "女";
stu2.stuNo = "90";
//实列化一个学生:张大朗
Student stu3 = new Student();
stu3.name = "二流子";
stu3.age = 19;
stu3.sex = "男";
stu3.stuNo = "70";
//将学生组合成数组,赋值给老师的数组变量。
tea.students[0] = stu1;
tea.students[1] = stu2;
tea.students[2] = stu3;
// 实例化一个老师
Teacher tea = new Teacher();
tea.name = "苍老师";
tea.age = 32;
tea.sex = "女";
tea.major = "电影演员";
tea.workYear = 6;
// 给学生中的教师变量赋值
stu1.teacher = tea;
stu2.teacher = tea;
stu3.teacher = tea;
// 苍老师的学生有谁
System.out.println(tea.name+"的学生有:");
for(Student stu : tea.students){
System.out.println(stu);
}
}
}
```