编程语言
首页 > 编程语言> > java--组合类的实现

java--组合类的实现

作者:互联网

何为组合类
一个类中含有另一个类

本程序为一个学生信息程序可访问老师的信息
好处:修改老师相关信息方便

package pac;

public class Combination1 {
	
	String name;
	String major;
	String ID;
	double GPA;
	Professor teacher;
	
	public Combination1(String na, String ma, String I, double J)
	{
		name = na;
		major = ma;
		ID = I;
		GPA = J;	
	}
	

}

package pac;

public class Professor {
	
	String name;
	String course;
	String email;
	String phone;
	
	public Professor(String na, String co, String em, String ph)
	{
		name =na;
		course = co;
		email = em;
		phone = ph;
	}
	
	public void coursetime()
	{
		System.out.println("class table");
	}

}

package pac;

public class Combanitiontest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Combination1 student = new Combination1("liuwanqing", "software", "20206960",3.8);
	    Professor teacher1 = new Professor("teachertan", "lisanshuxue", "123@qq.com", "12345");
	    student.teacher = teacher1;
	    System.out.println(student.teacher.course);
	    student.teacher.coursetime();
	

	}

}

坚持每天学一点,日更
生活充满迷茫,但只要坚持战斗,就没有输

标签:java,String,组合,--,na,Professor,teacher,student,public
来源: https://blog.csdn.net/liuwanqing233333/article/details/114946283