4-4 jmu-java-m04-Abstract、Person、Student、Teacher (10分)

该程序包含

Person类(抽象类):
属性:int id, String name, LocalDate birthDate。注意:定义时需按照顺序从上到下定义。
方法:构造方法(id, name, year, month, dayOfMonth); id与birthDate的getter方法; name的getter/setter方法;Eclipse自动生成的tostring;抽象方法String getPersonNumber(),该方法之所以声明为抽象是因为其在子类返回的信息与子类特有属性相关。

Student类继承自Person:
属性:int enrollmentYear, String clazzName。
方法:构造方法(id, name, year, month, dayOfMonth, enrollmentYear, clazzName),建议使用super复用Person的构造函数;
覆盖父类的 getPersonNumber()方法,返回"Student-enrollmentYear-name"

Teacher类继承自Person:
属性:String department, double salary。
方法:构造方法(id, name, year, month, dayOfMonth, department, salary),建议使用super复用Person的构造函数;
覆盖父类的 getPersonNumber()方法,返回"Teacher-department-name"

main方法包含:

public static void main(String[] args) {
String[] types = {“Student”,“Teacher”};
//输入种子seed,并用其初始化Random对象
//输入对象生成数量n
//以下是生成n个对象的循环
//使用random.nextInt(2)从types数组中随机选取元素x并输出
//根据x选择输入相应的参数
//输出n个对象getPersonNumber
//以下几行输出验证信息,请直接复制粘贴到你的代码中
System.out.println(Student.class.getSuperclass());
System.out.println(Teacher.class.getSuperclass());
try {
System.out.println(Person.class.getDeclaredMethod(“getPersonNumber”, null));
System.out.println(Student.class.getDeclaredMethod(“getPersonNumber”, null));
System.out.println(Teacher.class.getDeclaredMethod(“getPersonNumber”, null));
} catch (Exception e) {
e.printStackTrace();
}
}

输入格式:
随机数种子seed
产生对象数量n
n行,每行是每个对象的信息信息。
如果要生成Student对象则依次输入:姓名 年 月 日 入学年份 班级
如果要生成Teacher对象则依次输入:姓名 年 月 日 系 薪水
注意:每个对象的id为每次循环的序数,依次从0到n-1

输出格式:
n个对象的类型
n个对象getPersonNumber
验证信息

输入样例:
3
6
Liz 1986 10 10 CS 2000
Tom 1978 5 7 CS 2500.5
Sally 2000 1 3 2018 Net01
Jerry 1988 6 7 Mechanical 1990
Eliza 2001 2 5 2019 Software02
Li 1978 5 7 cs 2400

输出样例:
Teacher
Teacher
Student
Teacher
Student
Teacher
Teacher-CS-Liz
Teacher-CS-Tom
Student-2018-Sally
Teacher-Mechanical-Jerry
Student-2019-Eliza
Teacher-cs-Li
这里是验证信息

作者
郑如滨
单位
集美大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB

import java.util.*;
import java.time.LocalDate;

public class Main {
	public static void main(String[] args) {
    	Scanner sc=new Scanner(System.in);
    	String[] types = {"Student","Teacher"};
        int seed=sc.nextInt();
        Random r = new Random(seed);//输入种子seed,并用其初始化Random对象
        int n=sc.nextInt();//输入对象生成数量n
        Person[] p = new Person[n];
        String[] strr = new String[n];
        for(int i=0;i<n;i++)//以下是生成n个对象的循环
       {
    	   String str =types[r.nextInt(2)];
    	  //使用random.nextInt(2)从types数组中随机选取元素x并输出
    	   strr[i]=str;///
    	   //根据x选择输入相应的参数
    	   if(str.equals("Student"))
    	   {
    		   String name=sc.next();
    		   int year=sc.nextInt();
    		   int month=sc.nextInt();
    		   int dayOfMonth=sc.nextInt();
    		   int enrollmentYear=sc.nextInt();
   			   String clazzName=sc.next();
   			   Student s = new Student(i,name,year,month,dayOfMonth,enrollmentYear,clazzName); 
   			   p[i]=s;
    	   }
    	   else if(str.equals("Teacher"))
    	   {
    		   String name=sc.next();
    		   int year=sc.nextInt();
    		   int month=sc.nextInt();
    		   int dayOfMonth=sc.nextInt();
    		   String department=sc.next();
    		   double salary=sc.nextDouble();
    		   Teacher t = new Teacher(i,name,year,month,dayOfMonth,department,salary);
    		   p[i]=t;
    	   }
    	   
       }
        for(String f:strr)
        {
        	System.out.println(f);
        }
        
        for(Person x:p)
        {
        	System.out.println(x.getPersonNumber());
        }
    	//以下几行输出验证信息,请直接复制粘贴到你的代码中
        System.out.println(Student.class.getSuperclass());
        System.out.println(Teacher.class.getSuperclass());
        try {
           System.out.println(Person.class.getDeclaredMethod("getPersonNumber", null));
           System.out.println(Student.class.getDeclaredMethod("getPersonNumber", null));
           System.out.println(Teacher.class.getDeclaredMethod("getPersonNumber", null));
        } catch (Exception e) {
           e.printStackTrace();
        } 
      
        
	}

}
abstract class Person
{
	int id;
	String name; 
	LocalDate birthDate;
	int year;
	int month;
	int dayOfMonth;
	
	public Person(int id,String name,int year,int month,int dayOfMonth)
	{
		this.id=id;
		this.name=name;
		this.year=year;
		this.month=month;
		this.dayOfMonth=dayOfMonth;
		this.birthDate=LocalDate.of(this.year,this.month,this.dayOfMonth);
		
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public LocalDate getBirthDate() {
		return birthDate;
	}


	public String toString() {
		return "Person [id=" + id + ", name=" + name + ", birthDate=" + birthDate +  "]";
	}
	//抽象方法
	public abstract String getPersonNumber();
	
}

class Student extends Person
{
	int enrollmentYear;
	String clazzName;
	public Student(int id,String name,int year,int month,int dayOfMonth,int enrollmentYear,String clazzName)
	{
			super(id,name,year,month,dayOfMonth);//使用super复用Person的构造函数
			this.enrollmentYear=enrollmentYear;
			this.clazzName=clazzName;
			
	}
	public String getPersonNumber() {
		return "Student-"+this.enrollmentYear+"-"+this.name;
	}
	
}
class Teacher extends Person
{
	String department;
	double salary;
	
	public Teacher(int id,String name,int year,int month,int dayOfMonth,String department,double salary)
	{
		super(id,name,year,month,dayOfMonth);
		this.department=department;
		this.salary=salary;
	}
	public String getPersonNumber() {
		
		return "Teacher-"+this.department+"-"+this.name;
	}
}

你可能感兴趣的:(4-4 jmu-java-m04-Abstract、Person、Student、Teacher (10分))