对象中的对象10.15学习

对象中的对象
在对象中拥有属性,其属性类型是其他类。

对象数组:
数组的类型是某个类。数组中的值是对象。

package am1;

public class Students1 {

public String name;

public String sex;

public int age;

public int stuNo;//学分
	
//引用教师类
public Teacher1 teacher;

public String toString(){
	return"姓名"+name+"  性别"+sex+"\t年龄"+age+"\t学分"+stuNo+"\t老师姓名"+teacher.name;
}

}
package am1;

public class Teacher1 {

public String name;

public String sex;

public int age;

public int workYear;//工作年限

public String major;//专业

//对象数组
public Students1[] students = new Students1[3];

public String toString (){
	
	return "教室姓名"+name+"\t教师年龄"+age+"\t教师性别"+sex+"\t工作年限"+workYear+"\t专业"+major;
}
public void show(){
	//遍历数组
	for(int i =0;i < students.length;i++){
		//将数组中的对象,解析出来
		
		Students1 stu = students[i];
		
		System.out.println(stu);
	}
	
}

}

ackage am1;

import am.Student;

public class Test2 {

public static void main(String[] args) {
	//实例化一个学生: 张大郎
	Students1 stu1 = new Students1();
	stu1.name = "张大朗";
	stu1.age = 19;
	stu1.sex = "女";
	stu1.stuNo = 100;
	
	// 实例化一个学生:李大漂亮
	Students1 stu2 = new Students1();
	stu2.name = "李大漂亮";
	stu2.age = 17;
	stu2.sex = "男";
	stu2.stuNo = 80;
	
	// 实例化一个学生:李大漂亮
	Students1 stu3 = new Students1();
	stu3.name = "王铁蛋";
	stu3.age = 19;
	stu3.sex = "女";
	stu3.stuNo = 80;
	
	//实例化一个老师
	Teacher1 tea = new Teacher1();
	
	tea.name = "王老师";
	
	tea.age = 30;
	
	tea.major = "演员";
	
	tea.sex ="女";
	
	tea.workYear = 9;
	
	
	//给学生中的老师变量赋值;
	stu1.teacher = tea;
	stu2.teacher = tea;
	stu3.teacher = tea;
	//将学生组合成数组,赋值给老师的数组变量;
	//Student[] students = {stu1.stu2,stu3};
	//tea.students = students;
	tea.students[0] = stu1;
	
	tea.students[1] = stu2;
	
	tea.students[2] = stu3;
	
	//打印教师信息
	System.out.println(tea);
	//打印这个来哦是所有的学生信息;
	tea.show();
	
	//王铁蛋 的老师是谁?
	System.out.println(stu3.name+"的老师是:"+stu3.teacher.name);
	
	//王老师的学生有谁;
	System.out.println(tea.name+"的学生有:");
	for(Students1 stu : tea.students){
		
		System.out.println(stu);
	}
	
	
	
	
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值