关于JAVA泛型的例子

关于泛型的例子

编写简单的学生、教师管理程序,学生类、教师类有公共的父类: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();
		}
		
		}
	}
}
}




代码能够完成基础功能,还有些许不足,大家参考参考就好



下面是一个使用Java泛型例子,它定义了一个泛型类,可以接受任何类型的数据,并提供了一些通用的方法: ```java public class GenericClass<T> { private T data; public void setData(T data) { this.data = data; } public T getData() { return data; } public void printData() { System.out.println("Data: " + data); } public boolean isEqual(GenericClass<T> other) { return this.data.equals(other.getData()); } } ``` 在上面的例子中,我们使用了Java泛型来定义一个通用的类,这个类可以接受任何类型的数据。我们使用了类型参数T来表示这个通用类型,它可以是任何Java数据类型,如String、Integer、Double等。 在类定义中,我们使用了类型参数T来定义了一个私有数据成员data,它表示这个泛型类的数据。我们还提供了一些通用的方法,这些方法可以用于处理这个数据。例如,我们使用了setData和getData方法来设置和获取这个数据。我们还提供了一个printData方法,用于打印这个数据。最后,我们使用了isEqual方法来比较两个泛型类对象的数据是否相等。 在使用这个泛型类时,我们可以使用任何Java数据类型来实例化它。例如,我们可以使用以下代码来创建一个存储整数数据的泛型类对象: ```java GenericClass<Integer> intObj = new GenericClass<>(); intObj.setData(10); intObj.printData(); // 输出:Data: 10 ``` 我们还可以使用以下代码来创建一个存储字符串数据的泛型类对象: ```java GenericClass<String> strObj = new GenericClass<>(); strObj.setData("Hello, World!"); strObj.printData(); // 输出:Data: Hello, World! ``` 在这些例子中,我们使用了类型参数来指定泛型类的数据类型。这使得我们可以使用同一个泛型类来处理不同类型的数据,从而提高了代码的通用性和重用性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeYuZJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值