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(); }*/ } }
运行结果: