62,614
社区成员
发帖
与我相关
我的任务
分享
public class Person {
private String id;
public Person(String id) {
this.setId(id);
}
public Person() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
public class Student extends Person {
private String stuid;
public Student(String id, String stuid) {
this.setId(id);
this.setStuid(stuid);
}
public Student() {
}
public void introduce() {
System.out.println("我是一个学生!");
}
public String getStuid() {
return stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
}
public class PersonManage {
private static Person[] arr = new Person[3];
private static PersonManage manage;
private PersonManage(){
Person p1 = new Person("id1");
Person p2 = new Person("id2");
Person p3 = new Person("id3");
arr[0] = p1;
arr[1] = p2;
arr[2] = p3;
}
public static PersonManage getInstance(){
if(manage == null){
manage = new PersonManage();
}
return manage;
}
public Person getPerson(int idx){
if(idx > arr.length-1){
return null;
}
return arr[idx];
}
}
public class StudentManage {
private static Student[] arr = new Student[3];
private static StudentManage manage;
private StudentManage(){
Student s1 = new Student("id1", "stuid1");
Student s2 = new Student("id2", "stuid2");
Student s3 = new Student("id3", "stuid3");
arr[0] = s1;
arr[1] = s2;
arr[2] = s3;
}
public static StudentManage getInstance(){
if(manage == null){
manage = new StudentManage();
}
return manage;
}
public Student getStudent(int idx){
if(idx > arr.length-1){
return null;
}
return arr[idx];
}
}
/**
* 大学生类
*/
public class CollegeStudent extends Student {
public CollegeStudent(String id, String stuid) {
super(id, stuid);
}
public CollegeStudent(){}
@Override
public void introduce() {
System.out.println("我是一个大学生!");
}
}
/**
* 中学生类
*/
public class MiddleStudent extends Student {
public MiddleStudent(String id, String stuid) {
super(id, stuid);
}
public MiddleStudent(){}
@Override
public void introduce() {
System.out.println("我是一个中学生!");
}
}
/**
* 小学生类
*/
public class PrimaryStudent extends Student {
public PrimaryStudent(String id, String stuid) {
super(id, stuid);
}
public PrimaryStudent(){}
@Override
public void introduce() {
System.out.println("我是一个小学生!");
}
}
/**
* 多态类
*/
public class PolymorphismClass {
public void sayme(Student student){
student.introduce();
}
}
/**
* 测试
*/
public class Test0 {
public static void main(String[] args) {
//问题一
StudentManage manage = StudentManage.getInstance();
Student s = manage.getStudent(0);
System.out.println(s.getId() + " " + s.getStuid());
//问题二
CollegeStudent cs = new CollegeStudent();
MiddleStudent ms = new MiddleStudent();
PrimaryStudent ps = new PrimaryStudent();
PolymorphismClass poly = new PolymorphismClass();
poly.sayme(cs);
poly.sayme(ms);
poly.sayme(ps);
}
}