Java设计一个学生类Student,包含的属性有name和年龄age

由学生类派生出本科生类Undergraduate和研究生类Postgraduate,本科生类包含的属性由专业specialty,研究生包含的属性有研究方向studydirection。重写Student类中的displayInformation()方法进行相关数据的输出。
Student类:
package package1;

public class Student {
	private String name;
	private int age;
	private java.util.Date dateCreated;
	
	public Student() {
		dateCreated = new java.util.Date();
		name = "Susie";
		age = 19;
	}
	
	public Student(String name, int age) {
		dateCreated = new java.util.Date();
		this.name = name;
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	public java.util.Date getDateCreated(){
		return dateCreated;
	}
	
	public void displayinformation() {
		System.out.println("学生的姓名为:"+getName());
		System.out.println("学生的年龄为:"+getAge());
	}
	
	public String toString() {
		return "created on "+dateCreated+" name "+name+" and age: "+age;
	}
}

```本科生类:
package package1;

public class Undergraduate extends Student{
	private String specialty;
	
	public Undergraduate() {
		
	}
	
	public Undergraduate(String specialty) {
		this.specialty = specialty;
	}
	
	public Undergraduate(String specialty, String name, int age) {
		super(name, age);
		this.specialty = specialty;
	}
	
	public String getSpecialty() {
		return specialty;
	}
	
	public void setSpecialty(String specialty) {
		this.specialty = specialty;
	}
	
	@Override
	public void displayinformation() {
		System.out.println("学生的专业为:"+specialty);
	} 
}
研究生类:
package package1;

public class Postgraduate extends Student{
	private String studydirection;
	
	public Postgraduate() {
		
	}
	
	public Postgraduate(String studydirection) {
		this.studydirection = studydirection;
	}
	
	public Postgraduate(String studydirection, String name, int age) {
		super(name, age);
		this.studydirection = studydirection;
	}
	
	public String getStudydirection() {
		return studydirection;
	}
	
	public void setStudydirection(String studydirection) {
		this.studydirection = studydirection;
	}
	
	@Override
	public void displayinformation() {
		System.out.println("学生的研究方向为:"+studydirection);
	} 
}

主方法:
package package1;

public class TestStudent {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Undergraduate stu1 = new Undergraduate("computer science");
		stu1.displayinformation();
		System.out.println("undergraduate information is"+stu1.toString());
		System.out.println("undergraduate name is "+stu1.getName());
		System.out.println("undergraduate age is "+stu1.getAge());
		System.out.println("postgraduate specialty is "+stu1.getSpecialty());
		
		Postgraduate stu2 = new Postgraduate("artific intelligent");
		stu2.displayinformation();
		//System.out.println("postgraduate information is"+stu2.toString());
		System.out.println("postgraduate name is "+stu2.getName());
		System.out.println("postgraduate age is "+stu2.getAge());
		System.out.println("postgraduate direction is "+stu2.getStudydirection());
	}
}




你可能感兴趣的:(Java设计一个学生类Student,包含的属性有name和年龄age)