java是不支持在运行中进行强制转化的! 如果事先知道类型,可以通过强制转化实现! 如果不知道! 可以采用这种方式! \x0d\String className = objgetClass()getName(); 得到类名\x0d\然后:\x0d\A a = (A)ClassforName(className)newInstance(); \x0d\//上面这句话的意思是,forName(className):静态加载className这个类; \x0d\// newInstance(),实例化一个对象\x0d\// A 代表你知道的这人类或者这个类的父类, 把强制转化成你所要的对象。\x0d\这和你 \x0d\A a = new A(); \x0d\是一样的效果!
package test;
import javabeansIntrospectionException;
import javabeansPropertyDescriptor;
import javalangreflectInvocationTargetException;
import javalangreflectMethod;
import javautilArrayList;
import javautilArrays;
import javautilIterator;
public class AList extends ArrayList<A> {
/
/
private static final long serialVersionUID = -5626405721508120479L;
public A getSubject(String name, Object value)
throws IllegalAccessException, IllegalArgumentException,
InvocationTargetException, IntrospectionException {
A[] result = getSubjects(name, value);
if (resultlength <= 0)
return null;
else
return result[0];
}
public A[] getSubjects(String name, Object value)
throws IntrospectionException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(name, Aclass);
Method getter = pdgetReadMethod();
Iterator<A> it = thisiterator();
A[] result = new A[0];
while (ithasNext()) {
A obj = itnext();
Object theValue = getterinvoke(obj);
if (theValue != null && theValueequals(value)) {
result = ArrayscopyOf(result, resultlength + 1);
result[resultlength - 1] = obj;
}
}
return result;
}
public static void main(String[] args) throws IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
IntrospectionException {
AList al = new AList();
A a = new A("张三");
asetAge(18);
asetGender("男");
asetSchool("XX大学");
A b = new A("李四");
bsetAge(20);
bsetGender("男");
bsetSchool("XX大学");
A c = new A("王五");
csetAge(18);
csetGender("女");
csetSchool("XX大学");
aladd(a);
aladd(b);
aladd(c);
Systemoutprintln(ArraystoString(algetSubjects("name", "张三")));
Systemoutprintln(ArraystoString(algetSubjects("age", 18)));
Systemoutprintln(ArraystoString(algetSubjects("gender", "男")));
Systemoutprintln(ArraystoString(algetSubjects("school", "XX大学")));
// [A [name=张三, age=18, gender=男]]
// [A [name=张三, age=18, gender=男], A [name=王五, age=18, gender=女]]
// [A [name=张三, age=18, gender=男], A [name=李四, age=20, gender=男]]
// [A [name=张三, age=18, gender=男], A [name=李四, age=20, gender=男], A [name=王五, age=18, gender=女]]
}
}
class A {
private String name;
private int age;
private String gender;
private String school;
public String getSchool() {
return school;
}
public void setSchool(String school) {
thisschool = school;
}
public A(String name) {
super();
thisname = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
thisgender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
thisage = age;
}
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
@Override
public String toString() {
return "A [name=" + name + ", age=" + age + ", gender=" + gender + "]";
}
}
以上就是关于java 如何通过对象获取对象类全名全部的内容,包括:java 如何通过对象获取对象类全名、java根据对象属性从对象列表中取出对象、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)