关于泛型的例子
编写简单的学生、教师管理程序,学生类、教师类有公共的父类:Person,请添加相关属性。写泛型类 Person Manager,实现以对了学生、教师进行管理。PersonManager有方法:add(T t) ; remove( T t) , findById( int id), update( T t) , findAll(). 根据需要添加其他方法。通过键盘选择是对学生进行管理,或者是对教师进行管理,所有必须的信息都通过键盘录入。录入的数据存储在List对象中。
person类:
public class Person {
private String name;
private int age;
private String sex;
private int id;
public Person(String name,int age,int id,String sex) {
this.age = age;
this.name = name;
this.sex = sex;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
PersonManager类:
```java
import java.lang.reflect.Field;
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>{
ArrayList<T> list = new ArrayList<T>();
Scanner input = new Scanner(System.in);
public void add(T t) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
list.add(t);
System.out.println(list.size());
Class clazz = t.getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null){
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
clazz = clazz.getSuperclass();
}
Field[] fields = new Field[fieldList.size()];
fieldList.toArray(fields);
for(Field f1:fields) {
f1.setAccessible(true);
System.out.println(f1.get(list.get(0)));
System.out.println(f1.getName());
}
}
public void remove(T t) throws IllegalArgumentException, IllegalAccessException {
for (int i = 0;i < list.size();i++) {
T c = (T) list.get(i);
Class clazz = t.getClass();
int flag = 0;
List<Field> fieldList = new ArrayList<>();
while (clazz != null){
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
clazz = clazz.getSuperclass();
}
Field[] fields = new Field[fieldList.size()];
fieldList.toArray(fields);
for(Field f1:fields) {
f1.setAccessible(true);
if(f1.get(list.get(i)).equals(f1.get(t)) == false) {
flag = 1;break;
}
}
if(flag == 0) {
list.remove(c);
}
}
}
public <T>T findById(int id) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
for (int i = 0;i < list.size();i++) {
T t = (T) list.get(i);
Class clazz = t.getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null){
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
clazz = clazz.getSuperclass();
}
Field[] fields = new Field[fieldList.size()];
fieldList.toArray(fields);
for(Field f1:fields) {
f1.setAccessible(true);
if(f1.get(list.get(i)).equals(id)) {
return (T) list.get(i);
}
}
}
return null;
}
public void update(T t) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, InstantiationException {
System.out.println("请输入想要修改的学号:");
int x = input.nextInt();
T c = null;
for (int i = 0;i < list.size();i++) {
T q = (T) list.get(i);
Class clazz = t.getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null){
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
clazz = clazz.getSuperclass();
}
Field[] fields = new Field[fieldList.size()];
fieldList.toArray(fields);
for(Field f1:fields) {
f1.setAccessible(true);
if(f1.get(list.get(i)).equals(x)) {
c = (T) list.get(i);
}
}
}
list.remove(c);
list.add(t);
}
public void findAll() throws IllegalArgumentException, IllegalAccessException{
for(int i = 0;i <list.size();i++) {
Class clazz = list.get(i).getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null){
fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
clazz = clazz.getSuperclass();
}
Field[] fields = new Field[fieldList.size()];
fieldList.toArray(fields);
for(Field f1:fields) {
f1.setAccessible(true);
System.out.print(f1.getName() + " : " + f1.get(list.get(i)) + " ");
}
System.out.println();
}
}
}
student类:
public class Student extends Person{
private String classNo;
public Student(String name, int age,int id ,String sex) {
super(name, age,id, sex);
// TODO 自动生成的构造函数存根
}
public Student(String name,int age,int id,String sex,String classNo) {
super(name, age, id,sex);
this.classNo = classNo;
}
public String getClassNo() {
return classNo;
}
public void setClassNo(String classNo) {
this.classNo = classNo;
}
}
teacher类:
public class Teacher extends Person{
private String post;
public Teacher(String name, int age, int id,String sex) {
super(name, age, id,sex);
// TODO 自动生成的构造函数存根
}
public Teacher(String name, int age, int id,String sex,String post) {
super(name,age,id,sex);
this.post = post;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
}
test测试类:
import java.util.Scanner;
public class Test {
/**
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws SecurityException
*/
/**
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws SecurityException
*/
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
PersonManager per = new PersonManager();
Scanner input = new Scanner(System.in);
System.out.println("1.对教师进行管理");
System.out.print("2.对学生进行管理");
int choice = input.nextInt();
if(choice == 1) {
System.out.println("退出(0)");
while(true) {
System.out.println("1.增加");
System.out.println("2.删除");
System.out.println("3.修改");
System.out.println("4.查找");
System.out.println("5.查找所有");
int x = input.nextInt();
if(x == 0) {
break;
}
switch(x) {
case 1: System.out.println("请分别输入教师的姓名、年龄、id号、性别、职位");
String name = input.next();
int age = input.nextInt();
int id = input.nextInt();
String sex = input.next();
String post = input.next();
Teacher teacher = new Teacher(name,age,id,sex,post);
per.add(teacher);break;
case 2:System.out.println("请分别输入教师的姓名、年龄、id号、性别、职位");
name = input.next();
age = input.nextInt();
id = input.nextInt();
sex = input.next();
post = input.next();
teacher = new Teacher(name,age,id,sex,post);
per.remove(teacher);break;
case 3:System.out.println("请分别输入修改后教师的姓名、年龄、id号、性别、职位");
name = input.next();
age = input.nextInt();
id = input.nextInt();
sex = input.next();
post = input.next();
teacher = new Teacher(name,age,id,sex,post);
per.update(teacher);break;
case 4:System.out.println("请输入教师的id号");
id = input.nextInt();
teacher = (Teacher) per.findById(id);
System.out.println("id:" + teacher.getId() + " name:" + teacher.getName() + " age:" + teacher.getAge() + " sex:" + teacher.getSex() + " post:" + teacher.getPost());
break;
case 5:per.findAll();
}
}
}
else if(choice == 2) {
System.out.println("退出(0)");
while(true) {
System.out.println("1.增加");
System.out.println("2.删除");
System.out.println("3.修改");
System.out.println("4.查找");
System.out.println("5.查找所有");
int x = input.nextInt();
if(x == 0) {
break;
}
switch(x) {
case 1: System.out.println("请分别输入学生的姓名、年龄、id号、性别、班级号");
String name = input.next();
int age = input.nextInt();
int id = input.nextInt();
String sex = input.next();
String classNo = input.next();
Student student = new Student(name,age,id,sex,classNo);
per.add(student);break;
case 2:System.out.println("请分别输入教师的姓名、年龄、id号、性别、班级号");
name = input.next();
age = input.nextInt();
id = input.nextInt();
sex = input.next();
classNo = input.next();
student = new Student(name,age,id,sex,classNo);
per.remove(student);break;
case 3:System.out.println("请分别输入修改后教师的姓名、年龄、id号、性别、职位");
name = input.next();
age = input.nextInt();
id = input.nextInt();
sex = input.next();
classNo = input.next();
student = new Student(name,age,id,sex,classNo);
per.update(student);break;
case 4:System.out.println("请输入学生的id号");
id = input.nextInt();
student = (Student) per.findById(id);
System.out.println("id:" + student.getId() + " name:" + student.getName() + " age:" + student.getAge() + " sex:" + student.getSex() + " classNo:" + student.getClassNo());
break;
case 5:per.findAll();
}
}
}
}
}
代码能够完成基础功能,还有些许不足,大家参考参考就好