testpeople.java

来自「很好的一本书《JAVA设计模式》的随书源码5」· Java 代码 · 共 57 行

JAVA
57
字号
/* File Name:TestPeople.java
* 这是关于方法重载的综合应用实例,用来说明构造函数的重载与继承等的用法。
*/
class People {
	long Num;
	String Name;
	String Sex;
	int Ages;
	public People(long Num,String Name) {
		this.Num=Num;
		this.Name=Name;
	}
	public People(long Num,String Name,String Sex,int Ages) {
		this(Num,Name);
		this.Sex=Sex;
		this.Ages=Ages;
	}
	public String ShowPeople() {
		return(" 编号:"+Num+" 姓名:"+Name+" 性别:"+Sex+" 年龄:"+Ages);
	}
	public String ShowPeople(long Num1,String Name1) {
		Num=Num1;
		Name=Name1;
		return(" 编号:"+Num+" 姓名:"+Name);
	}
}
class Student extends People
{
	String ClassNo;
	public Student(long Num,String Name,String Sex,int Ages,String ClassNo) {
		super(Num,Name,Sex,Ages);
		this.ClassNo=ClassNo;
	}
	public String ShowStudent() {
		return(ShowPeople()+" 班号:"+ClassNo);
	}
}
class Teacher extends People {
	String Principalship;
	String Department;
	public Teacher(long Num,String Name,String Principalship,String Department) {
		super(Num,Name);
		this.Principalship=Principalship;
		this.Department=Department;
	}
	public String ShowTeacher() {
		return(ShowPeople(Num,Name)+" 职务:"+Principalship+" 部门:"+Department);
	}
}
public class TestPeople {
	public static void main(String[] args) {
		Student NewStudent=new Student(10010,"王文","男",22,"商务001");
		System.out.println(NewStudent.ShowStudent());
		Teacher NewTeacher=new Teacher(50030,"李黎","教师","商务系");
		System.out.println(NewTeacher.ShowTeacher());
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?