例如一个方法传入一个参数,这个参数是某类的属性,通过这样的条件取这个类属性的值
List<T> a = new ArrayList<T>();
String A ="id";
求出泛型T类A属性的值。
PS:就是通过泛型T,实例化这个类 ,或者通过反射找出这个T.A的值..求指教啊
一般不用T.A来求值,用get方法比较好。我写了个简单的例子,你看看。
public interface A {public String getName();
}
public class B implements A {
private final String name = "AB";
@Override
public String getName() {
return this.name;
}
public String getB() {
return "Method only in B";
}
}
public class C implements A {
private final String name = "AC";
@Override
public String getName() {
return this.name;
}
public String getC() {
return "Method only in C";
}
}
public class Main {
public <T extends A> void show(T t) {
System.out.println(t.getName());
if (t instanceof B) {
System.out.println(((B) t).getB());
}
if (t instanceof C) {
System.out.println(((C) t).getC());
}
System.out.println();
}
public static void main(String[] args) {
final Main main = new Main();
main.show(new B());
main.show(new C());
}追问
我写了个类继承了ArrayList 作用是 写个方法,找出这个LIst中,对象的属性是A,值为B 的对象,返回个List 给我
public List findV(String A,StringB)
{
//实例化T
//遍历,并 反射找出T 的属性为A 值为B 的对象集合 返回
}
public class Main {
private final MyList list = new MyList();
public Main() {
this.list.add(new B());
this.list.add(new C());
}
public List<A> findV(String value) {
final List<A> res = new MyList();
for (A element : this.list) {
if (element.getName().equals(value))
res.add(element);
}
return res;
}
public static void main(String[] args) {
final Main main = new Main();
final List<A> res = main.findV("AB");
System.out.println("Size: " + res.size());
for (A element : res) {
System.out.println("Name: " + element.getName());
}
}
}
对象的属性是A,值为B 的对象。用 findV(String A, String B)方法并不好。
对象的属性是A,就用getA(),所以findV(StringB)就够了, 比较用 getA().equals(B)。
追问...关键是不晓得属性是A 啊,我想写个共同的以后都能用...
所以呢,随便哪个类CLAS,
被我的List类 Add了,你给我个CLAS属性V,你给我个 值B,我就能找出,这个List中 CLAS对象属性的值 V等于B的所有对象
。。是不是我表述的有问题,相当于一个List中截取满足你输入对象的属性的V 值为B 的所有 List
只要弄明白的你的意思。我肯定做的出。追问
就是这样的,我写个A类继承ArrayList类,然后写个方法
public List findV(String A,String B)
我有个学生类,属性有 学生Id,学生姓名
然后把多个学生对象放入我的A 的List中
调用方法,找出
List s =findV("id","3") 找出id是3的学生
在findV中 肯定要实例化V ...问题来了。不懂再问 谢谢了
public List findV(String A,String B) throws IllegalArgumentException, IllegalAccessException{
List list1=new ArrayList();
for (UserEntity userEntity : list) {
for (Field f : UserEntity.class.getDeclaredFields()) {
f.setAccessible(true);
if (A.equals(f.getName())&&B.equals(f.get(userEntity).toString())) {
T e=(T) userEntity;
list1.add(e);
}
}
}
return list1;
}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
MyTest3 my=new MyTest3();
UserEntity u=new UserEntity();
u.setId(3);
u.setName("haha");
UserEntity u1=new UserEntity();
u1.setId(3);
u1.setName("hehe");
UserEntity u2=new UserEntity();
u2.setId(4);
u2.setName("heihei");
list.add(u);
list.add(u1);
list.add(u2);
List listUser=my.findV("id", "3");
for (int i = 0; i < listUser.size(); i++) {
System.err.println(listUser.get(i).getId());
}
}
首先非常感谢你写这么详细,其次,我希望这个写的更加共同一点,所以你的 findV
方法中的foreach 中的UserEntity 是不能写的,只能写T 的实例类,所以才有了我的问题,怎去实例化泛型类
15分太少。
追问不要意思啊....就这分了啊 大神
追答好吧
public class TTest {
public List list = new ArrayList();
public static void main(String[] args) throws IllegalArgumentException,
IllegalAccessException, InstantiationException {
TTest my = new TTest();
UserEntity u = new UserEntity();
u.setId(3);
u.setName("haha");
UserEntity u1 = new UserEntity();
u1.setId(3);
u1.setName("hehe");
UserEntity u2 = new UserEntity();
u2.setId(4);
u2.setName("heihei");
my.list.add(u);
my.list.add(u1);
my.list.add(u2);
List listUser = my.findV("id", "3");
for (int i = 0; i findV(String A, String B)
throws IllegalArgumentException, IllegalAccessException,
InstantiationException {
List list1 = new ArrayList();
for (T t1: list) {
for (Field f : t1.getClass().getDeclaredFields()) {
f.setAccessible(true);
if (A.equals(f.getName())&& B.equals(f.get(t1).toString())) {
list1.add(t1);
}
}
}
return list1;
}
}