反射(承接上面的泛型注解与反射)
Posted sunxiaoyan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了反射(承接上面的泛型注解与反射)相关的知识,希望对你有一定的参考价值。
1.
User.java
package com.inspur.demo3; public class User { private String name; private String sex; private int age; public String getName() { return name; } public User() { // TODO Auto-generated constructor stub } public User(String name){ this.name=name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString(){ return this.name+","+this.sex+","+this.age; } }
2.获取私有属性
3.
MyClass.java
package com.inspur.demo4; public class MyClass<A extends Integer,B,C> { }
DemoMain4.java
package com.inspur.demo4; import java.lang.reflect.Field; import java.util.Arrays; import com.inspur.demo3.User; public class DemoMain4 { public static void main(String[] args) { // TODO Auto-generated method stub try { /*Class c = User.class; Field f = c.getDeclaredField("name"); System.out.println(f.getName()); System.out.println(f.get(new User("xiaohei")));*/ Class c1 = MyClass.class; System.out.println(c1.getTypeParameters().length); System.out.println(c1.getTypeParameters()[0].getName()); System.out.println(c1.getTypeParameters()[0].getBounds()[0]); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } */catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } /*catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ } }
运行结果:
以上是关于反射(承接上面的泛型注解与反射)的主要内容,如果未能解决你的问题,请参考以下文章