类的继承与多态

类的继承与多态_第1张图片

 其中:
Person类具有String属性name(姓名)和display()方法;
Teacher类继承Person类,并具有String属性professionalTitle(职称)和display()方法;
Student类继承Person类,并具有int属性studentNumber(学号)和display()方法;
Graduate类和Undergraduate类都继承Student类,并具有各自的display()方法,
    Graduate类具有String属性major(专业),
    Undergraduate类具有int属性grade(年级)。
所有类都应该具有合适的构造方法和按照一定格式(见输出样例)输出所有属性值display()方法。

【输入形式】
【输出形式】
【样例输入】
【样例输出】

Name: Tom
>>>
Name: Kevin
Professional Title: professor
>>>
Name: Jerry
StudeneNumber: 21
>>>
Name: John
StudeneNumber: 22
Grade: 3
>>>
Name: Mary
StudeneNumber: 23
Major: computer
>>>

 

package day4231;

public class Graduate extends Student{
	private String major;
	public Graduate(String name, int studentNumber,String major) {
		super(name, studentNumber);
		this.major=major;
		
	}
	public void display(){
		super.display();System.out.println("Major: "+major);
	}

}







package day4231;

public class Undergraduate extends Student{
	private int grade;

	public Undergraduate(String name, int studentNumber,int grade) {
		super(name, studentNumber);
		this.grade=grade;
	}
	public void display(){
		super.display();
		System.out.println("Grade: "+grade);
	}

}







package day4231;

public class Student extends Person{
	private int studentNumber;
	public Student(String name,int studentNumber) {
		super(name);
		this.studentNumber=studentNumber;
	}
	public void display(){
		super.display();
		System.out.println("StudeneNumber: "+studentNumber);
		
	}

}








package day4231;

public class Teacher extends Person{

	private String professionTitle;
	public Teacher(String name,String professionTitle) {
		super(name);
		this.professionTitle=professionTitle;
		
	}
	public void display(){
		super.display();
		System.out.println("Professional Title: "+professionTitle);
		
	}
 
}









package day4231;

public class Person {
	private String name;
	
	public Person(String name) {
		super();
		this.name = name;
	}

	public void display(){
		System.out.println("Name: "+name);
	}

}






package day4231;

public class Test 
{
    public static void main(String[] args) 
    {
        Person[] people = new Person[5];
        people[0] = new Person("Tom");
        people[1] = new Teacher("Kevin","professor");
        people[2] = new Student("Jerry",21);
        people[3] = new Undergraduate("John",22,3);
        people[4] = new Graduate("Mary",23,"computer");

        for (Person p: people)
        {
            p.display();
            System.out.println(">>>");
        }
    }
}








 

你可能感兴趣的:(类的继承与多态)