java获取类内容
-
-
- Book类
-
public class Book implements Serializable { private int id; private String name; private int tid; private double price; private String author; private String descri; private String photo; private Date pubdate; public static final double PI=3.14; //记住对方 private Type type; public Book() {//公有的 } Book(int id){//包可见性 this.id=id; } public Type getType() { return type; } public void setType(Type type) { this.type = type; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getDescri() { return descri; } public void setDescri(String descri) { this.descri = descri; } public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } public Date getPubdate() { return pubdate; } public void setPubdate(Date pubdate) { this.pubdate = pubdate; } private void test(){} @Override public String toString() { return "Book [id=" + id + ", name=" + name + ", tid=" + tid + ", price=" + price + ", author=" + author + ", descri=" + descri + ", photo=" + photo + ", pubdate=" + pubdate + "]"; } }
-
-
- 看父类,实现的接口和构造函数
-
public class ReflectDemo01 { public static void main(String[] args) throws NoSuchMethodException, SecurityException { //照相法 1:通过静态属性 class //该方式在源代码中规定好了看那个类,不能在运行时动态改变 Class clazz1=Book.class; System.out.println("看父类"); System.out.println(clazz1.getSuperclass()); System.out.println("看实现了那些接口"); Class[] cls=clazz1.getInterfaces(); for (Class class1 : cls) { System.out.println(class1); } System.out.println("看所有构造函数"); Constructor[] cs1=clazz1.getDeclaredConstructors(); for (Constructor constructor : cs1) { System.out.println(constructor); } System.out.println("看所有 public 的函数"); Constructor[] cs2=clazz1.getConstructors(); for (Constructor constructor : cs2) { System.out.println(constructor); } System.out.println("看某个公有的"); //Constructor cons1=clazz1.getConstructor(int.class); //System.out.println(cons1); Constructor cons2=clazz1.getConstructor(); System.out.println(cons2); System.out.println("看某个"); Constructor cons3=clazz1.getDeclaredConstructor(int.class); System.out.println(cons3); } }
-
-
- 看方法
-
public class ReflectDemo02 { public static void main(String[] args) throws ClassNotFoundException, IOException, NoSuchMethodException, SecurityException { // 该方式在源代码中规定好了看那个类,不能在运行时动态改变 Class clazz1 = Book.class; // 看所有公有方法:包括继承 Method[] ms1 = clazz1.getMethods(); for (Method method : ms1) { System.out.println(method); } // 看本类定义的方法:不包括继承 System.out.println("--------------------"); Method[] ms2 = clazz1.getDeclaredMethods(); for (Method method : ms2) { System.out.println(method); } System.out.println("++++++++++++++++++++++"); // 看某个公有的 Method m1 = clazz1.getMethod("setPubdate", Date.class); System.out.println(m1); // Method m2=clazz1.getMethod("test"); // System.out.println(m2); System.out.println("============================="); // 看本类定义的方法 Method m3 = clazz1.getDeclaredMethod("setPubdate", Date.class); System.out.println(m3); Method m4=clazz1.getDeclaredMethod("test"); System.out.println(m4); } }
-
-
- 看字段
-
public class ReflectDemo03 { public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException { Class clazz1 = Book.class; // 看所有公有字段,包括继承的 Field[] fs1 = clazz1.getFields(); for (Field field : fs1) { System.out.println(field); } System.out.println("==========================="); // 看本类定义的 Field[] fs2 = clazz1.getDeclaredFields(); for (Field field : fs2) { System.out.println(field); } System.out.println("==========================="); // 看本类定义的某个字段 Field field1 = clazz1.getDeclaredField("name"); System.out.println(field1); // 看公有字段 //下面两行代码抛出异常:Exception in thread "main" java.lang.NoSuchFieldException: name //Field field2 = clazz1.getField("name"); //System.out.println(field2); } }
构造函数的调用
- 通用方法
public class ReflectDemo01 {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
// 1 找到像
Class<Book> clazz1 = Book.class;
// 2 找到构造函数
Constructor<Book> cs = clazz1.getDeclaredConstructor(int.class);
System.out.println(cs);
cs.setAccessible(true);//不设置访问不了,因为不再同一个包,修改这种行为
// 3 调用构造函数
Book book = cs.newInstance(1);
System.out.println(book);
}
}
- 无参函数调用
public class ReflectDemo02 {
public static void main(String[] args) throws NoSuchMethodException,
SecurityException, InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
// 1 找到像
Class<Book> clazz1 = Book.class;
// 2 找到构造函数
Constructor<Book> cs = clazz1.getDeclaredConstructor();
// 3 调用构造函数
Book book = cs.newInstance();
System.out.println(book);
//为了简化调用无参数构造函数,可以直接调用像的方法
Book book2=clazz1.newInstance();
System.out.println(book2);
}
}