关于泛型的例子
编写简单的学生、教师管理程序,学生类、教师类有公共的父类:Person,请添加相关属性。写泛型类 Person Manager,实现以对了学生、教师进行管理。PersonManager有方法:add(T t) ; remove( T t) , findById( int id), update( T t) , findAll(). 根据需要添加其他方法。通过键盘选择是对学生进行管理,或者是对教师进行管理,所有必须的信息都通过键盘录入。录入的数据存储在List对象中。
Person 类
package test3.question4;
public class Person {
private String name;
private String gender;
private Integer age;
public Person() {
}
public Person(String name, String gender, Integer age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
student类
package test3.question4;
public class Student extends Person{
private Integer stuId;
private Integer classId;
private String profession;
public Student() {
}
public Student(String name, String gender, Integer age, Integer stuId, Integer classId, String profession) {
super(name, gender, age);
this.stuId = stuId;
this.classId = classId;
this.profession = profession;
}
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public Integer getClassId() {
return classId;
}
public void setClassId(Integer classId) {
this.classId = classId;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", classId=" + classId +
", profession='" + profession + '\'' +
'}';
}
}
Teacher类
package test3.question4;
public class Teacher extends Person{
private Integer teacherId;
private String teachSubject;
public Teacher() {
}
public Teacher(String name, String gender, Integer age, Integer teacherId, String teachSubject) {
super(name, gender, age);
this.teacherId = teacherId;
this.teachSubject = teachSubject;
}
public Integer getTeacherId() {
return teacherId;
}
public void setTeacherId(Integer teacherId) {
this.teacherId = teacherId;
}
public String getTeachSubject() {
return teachSubject;
}
public void setTeachSubject(String teachSubject) {
this.teachSubject = teachSubject;
}
@Override
public String toString() {
return "Teacher{"+
"teacherId=" + teacherId +
", 所教的科目是:'" + teachSubject + '\'' +
'}';
}
}
PersonMananger类
package test3.question4;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class PersonManager<T> {
private List<T> personList = new ArrayList<>();
/**添加人员的操作*/
public void add(T t) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
addAtrribute(t);
personList.add(t);
}
/**删除人的操作*/
public void remove(T t) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Scanner input = new Scanner(System.in);
Class clazz = t.getClass();
// (());
String bigName = "";
if(clazz.getSimpleName().equals("Teacher")){
System.out.print("请输入要删除的教师的教师号:");
int id = input.nextInt();
Teacher teacher = (Teacher) findById(id);
if (teacher != null){
personList.remove(teacher);
}else{
System.out.println("查无此人");
}
}else if(clazz.getSimpleName().equals("Student")){
System.out.print("请输入要删除的学生的学号:");
int id = input.nextInt();
Student student = (Student) findById(id);
if (student != null){
personList.remove(student);
}else{
System.out.println("查无此人");
}
}
// if(isExist(t)){
// (t);
// }else{
//
// }
}
/**根据id找人*/
public<T> T findById(int id) throws IllegalAccessException {
for (int i = 0; i < personList.size(); i++) {
T person = (T)personList.get(i);
Class clazz = person.getClass();
List<Field> allFieldList = getAllField(clazz);
for (Field field:allFieldList){
field.setAccessible(true);
if(field.get(personList.get(i)).equals(id)){
return person;
}
}
}
return null;
}
/**更新*/
public void update(T t) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
if(isExist(t)){
personList.remove(t);
add(t);
}else{
System.out.println("查无此人");
}
}
/** 查找所有*/
public List<T> findAll(int x){
List<T> list = new ArrayList<>();
if(x == 1){
for(int i = 0;i < personList.size();i++){
if (personList.get(i).getClass().getSimpleName().equals("Teacher")) {
list.add(personList.get(i));
}
}
}
if (x == 2){
for(int i = 0;i < personList.size();i++){
if (personList.get(i).getClass().getSimpleName().equals("Student")) {
list.add(personList.get(i));
}
}
}
return list;
}
/**检验是否存在此人*/
public boolean isExist(T t) throws IllegalAccessException {
T person = null;
if(t.getClass().equals("Student")){
Student student = (Student)t;
person = findById(student.getStuId());
}else{
Teacher teacher = (Teacher)t;
person = findById(teacher.getTeacherId());
}
if (person == null){
return false;
}else{
return true;
}
}
//获取所有属性的方法
private List getAllField(Class clazz){
/**获取所有的属性*/
List<Field> fieldList = new ArrayList<>();
while(clazz != null){
Field[] fields = clazz.getDeclaredFields();
for(Field field:fields){
fieldList.add(field);
}
clazz = clazz.getSuperclass();
}
return fieldList;
}
/** 添加属性方法*/
private void addAtrribute(T t) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Scanner input = new Scanner(System.in);
Class clazz = t.getClass();
// (());
String bigName = "";
if(clazz.getSimpleName().equals("Teacher")){
bigName = "教师";
}else if(clazz.getSimpleName().equals("Student")){
bigName = "学生";
}
/**获取所有的属性*/
List<Field> fieldList = getAllField(clazz);
for (Field field:fieldList) {
String fieldName = field.getName();
System.out.print("请输入此" + bigName + "的" + fieldName + ":");
Object value = null;
if (field.getType().equals(String.class)) {
value = input.next();
} else if (field.getType().equals(Integer.class)) {
value = input.nextInt();
} else if (field.getType().equals(Float.class)) {
value = input.nextFloat();
} else if (field.getType().equals(Double.class)) {
value = input.nextDouble();
} else if (field.getType().equals(Boolean.class)) {
value = input.nextBoolean();
}
String oldFirstChar = String.valueOf(fieldName.charAt(0));
String setterName = "set" + fieldName.replaceFirst(oldFirstChar,oldFirstChar.toUpperCase());
// (setterName);
Method method = clazz.getMethod(setterName,field.getType());
method.invoke(t,value);
}
}
public List<T> getPersonList() {
return personList;
}
public void setPersonList(List<T> personList) {
this.personList = personList;
}
}
测试类
package test3.question4;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Scanner;
public class TestManager {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
PersonManager<Person> personPersonManager = new PersonManager<>();
Scanner input = new Scanner(System.in);
while(true){
System.out.println("1.对教师进行管理");
System.out.println("2.对学生进行管理");
System.out.println("3.退出管理界面");
System.out.print("请输入您的选择:");
int choice = input.nextInt();
if (choice == 1){
while(true){
System.out.print(
"1.添加教师\n" +
"2.删除教师\n" +
"3.根据教师号查找老师\n" +
"4.查找所有教师\n" +
"5.退出\n" + "请选择您要进行的操作:");
int choose = input.nextInt();
if (choose == 5){
break;
}
switch (choose){
case 1:
Teacher teacher = new Teacher();
personPersonManager.add(teacher);
break;
case 2:
Teacher teacher1 = new Teacher();
personPersonManager.remove(teacher1);
break;
case 3:
System.out.print("请输入教师号:");
int id = input.nextInt();
Teacher byId = (Teacher) personPersonManager.findById(id);
System.out.println(byId);
break;
case 4:
List<Person> list = personPersonManager.findAll(1);
if (list.size() != 0){
for (int i = 0; i < list.size(); i++) {
Teacher teacher2 = (Teacher)list.get(i);
System.out.println(teacher2.toString());
}
}else{
System.out.println("没有教师存在!");
}
break;
}
}
}
if (choice == 2){
while(true){
System.out.print(
"1.添加学生\n" +
"2.删除学生\n" +
"3.根据学生号查找学生\n" +
"4.查找所有学生\n" +
"5.退出\n" +
"请选择您要进行的操作:");
int choose = input.nextInt();
if (choose == 5){
break;
}
switch (choose){
case 1:
Student student = new Student();
personPersonManager.add(student);
break;
case 2:
Student student1 = new Student();
personPersonManager.remove(student1);
break;
case 3:
System.out.print("请输入学号:");
int id = input.nextInt();
Student byId = (Student) personPersonManager.findById(id);
System.out.println(byId);
break;
case 4:
List<Person> list = personPersonManager.findAll(2);
if (list.size() != 0){
for (int i = 0; i < list.size(); i++) {
Student student2 = (Student) list.get(i);
System.out.println(student2.toString());
}
}else{
System.out.println("没有学生存在!");
}
break;
}
}
}
if (choice == 3){
break;
}
}
}
}