设计一个java程序,如下:1 Person类(抽象类): 属性:int id, String name, LocalDate birthDate。 方法:构造方法(id, name, year, month, dayOfMonth); id与birthDate的getter方法; name的getter/setter方法; 抽象方法String getPersonInfo(),该方法之所以声明为抽象是因为其在子类返回的信息与子类特有属性相关。 Student类继承自Person类: 属性:int enrollmentYear, String clazzName。 方法:构造方法(id, name, year, month, dayOfMonth, enrollmentYear, clazzName),建议使用super复用Person的构造方法; 覆盖父类的 getPersonInfo()方法,返回"Student-enrollmentYear-name" Teacher类继承自Person: 属性:String department, double salary。 方法:构造方法(id, name, year, month, dayOfMonth, department, salary),建议使用super复用Person的构造方法; 覆盖父类的 getPersonInfo()方法,返回"Teacher-department-name" main方法包含: 输入 : 产生n个对象 如果要生成Student对象则依次输入:姓名 年 月 日 入学年份 班级 如果要生成Teacher对象则依次输入:姓名 年 月 日 系 薪水 输出 : 学生人数,教师人数 n个对象getPersonNumber 输入样例: 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 4人 Student 2人 Teacher-CS-Liz Teacher-CS-Tom Student-2018-Sally Teacher-Mechanical-Jerry Student-2019-Eliza Teacher-cs-Li
时间: 2023-06-19 19:06:15 浏览: 81
以下是Java代码实现:
```java
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Person> persons = new ArrayList<>();
System.out.print("请输入对象的个数:");
int n = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < n; i++) {
System.out.print("请输入对象类型(1为学生,2为教师):");
int type = scanner.nextInt();
scanner.nextLine();
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入出生年份:");
int year = scanner.nextInt();
System.out.print("请输入出生月份:");
int month = scanner.nextInt();
System.out.print("请输入出生日:");
int dayOfMonth = scanner.nextInt();
if (type == 1) {
System.out.print("请输入入学年份:");
int enrollmentYear = scanner.nextInt();
System.out.print("请输入班级名称:");
String clazzName = scanner.next();
persons.add(new Student(name, year, month, dayOfMonth, enrollmentYear, clazzName));
} else if (type == 2) {
System.out.print("请输入系别名称:");
String department = scanner.next();
System.out.print("请输入薪水:");
double salary = scanner.nextDouble();
persons.add(new Teacher(name, year, month, dayOfMonth, department, salary));
}
}
int studentCount = 0;
int teacherCount = 0;
for (Person person : persons) {
if (person instanceof Student) {
studentCount++;
} else if (person instanceof Teacher) {
teacherCount++;
}
System.out.println(person.getPersonInfo());
}
System.out.println("学生人数:" + studentCount);
System.out.println("教师人数:" + teacherCount);
}
}
abstract class Person {
protected int id;
protected String name;
protected LocalDate birthDate;
public Person(int id, String name, int year, int month, int dayOfMonth) {
this.id = id;
this.name = name;
this.birthDate = LocalDate.of(year, month, dayOfMonth);
}
public int getId() {
return id;
}
public LocalDate getBirthDate() {
return birthDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public abstract String getPersonInfo();
}
class Student extends Person {
private int enrollmentYear;
private String clazzName;
public Student(String name, int year, int month, int dayOfMonth, int enrollmentYear, String clazzName) {
super(0, name, year, month, dayOfMonth);
this.enrollmentYear = enrollmentYear;
this.clazzName = clazzName;
}
@Override
public String getPersonInfo() {
return "Student-" + enrollmentYear + "-" + name;
}
}
class Teacher extends Person {
private String department;
private double salary;
public Teacher(String name, int year, int month, int dayOfMonth, String department, double salary) {
super(0, name, year, month, dayOfMonth);
this.department = department;
this.salary = salary;
}
@Override
public String getPersonInfo() {
return "Teacher-" + department + "-" + name;
}
}
```
阅读全文