反射的相关知识汇总
1反射机制是什么
反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
2反射机制能做什么
反射机制主要提供了以下功能:
-
在运行时判断任意一个对象所属的类;
-
在运行时构造任意一个类的对象;
-
在运行时判断任意一个类所具有的成员变量和方法;
-
在运行时调用任意一个对象的方法;
-
生成动态代理。
3反射机制的相关API
通过一个对象获得完整的包名和类名
实例化Class类对象
public class test_3 { public static void main(String[] args) throws ClassNotFoundException { Class<?> class1 = Class.forName("com.training.Book"); Class<?> class2 = new Book().getClass(); Class<?> class3 = Book.class; System.out.println(class1.getName()); System.out.println(class2.getName()); System.out.println(class3.getName()); } } class Book{ String title; double price; public Book() { // TODO Auto-generated constructor stub } public Book(String title,double price) { // TODO Auto-generated constructor stub this.title=title; this.price=price; } public void print(){ System.out.println("111"); } public void fun(){} }
基础代码:
class Book{ String title; double price; public Book() { // TODO Auto-generated constructor stub } public Book(String title,double price) { // TODO Auto-generated constructor stub this.title=title; this.price=price; } public void setTitle(String title) { this.title = title; } public void setPrice(double price) { this.price = price; } public String getTitle() { return title; } public double getPrice() { return price; } public void print(){ System.out.println("111"); } public void fun(){} } interface BookMaker{ public static final String AUTHORD="Redick"; public static final String DATE="20170330"; public abstract String publiched(); } class StudyBook extends Book implements BookMaker{ private String name; public StudyBook() { // TODO Auto-generated constructor stub } public StudyBook(String name) { // TODO Auto-generated constructor stub this.name=name; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public String publiched() { // TODO Auto-generated method stub return "已出版的书名为:" +this.title+"\n"+"价格为:"+this.price+"\n"+"小名为:"+this.name; } @Override public void print() { // TODO Auto-generated method stub super.print(); } public void sell(int num){ System.out.println("卖了"+num+"本书"); } }
获取该类的父类和接口:
public class test_3 { public static void main(String[] args) throws ClassNotFoundException { Class<?> class1 = Class.forName("com.training.StudyBook"); Class<?> con1 = class1.getSuperclass(); Class<?> con2[] = class1.getInterfaces(); System.out.println("父类:"+con1.getName()); for(int i=0;i<con2.length;i++){ System.out.println("接口:" +con2[i].getName()); } } }
获取所有的构造函数和其对应的参数:
public class test_3 { public static void main(String[] args) throws ClassNotFoundException { Class<?> class1 = Class.forName("com.training.StudyBook"); Constructor<?> con1[] = class1.getConstructors(); for(int i=0;i<con1.length;i++){ System.out.print("构造函数"+(i+1)+":"+con1[i].getName()); Class<?> para[] = con1[i].getParameterTypes(); System.out.print("\t"+"此构造函数的参数为:"); if(para.length!=0){ for(int j=0;j<para.length;j++){ System.out.print(para[j].getName()+"、"); } }else{ System.out.print("该构造函数没有参数"); } System.out.println(); } } }
利用反射实例化对象:
public class test_3 { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class<?> class1 = Class.forName("com.training.StudyBook"); StudyBook ssBook = (StudyBook)class1.newInstance(); } }
获取类的全部属性:
public class test_3 { public static void main(String[] args) throws ClassNotFoundException { Class<?> class1 = Class.forName("com.training.StudyBook"); //getFields()返回一个包含某些 Field 对象的数组,这些对象反映此 Class 对象所表示的类或接口的所有可访问公共字段。 Field ffField[] = class1.getFields(); System.out.println("getFields()"); for(int i=0;i<ffField.length;i++){ System.out.println(ffField[i].getName()); } //getDeclaredFields() 返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段。 Field ssField[] = class1.getDeclaredFields(); System.out.println("getDeclaredFields()"); for(int j=0;j<ssField.length;j++){ System.out.println(ssField[j].getName()); } } }
通过反射调用某个方法:
public class test_3 { public static void main(String[] args) throws Exception { Class<?> class1 = Class.forName("com.training.StudyBook"); Method mm2 =class1.getMethod("print"); mm2.invoke(class1.newInstance()); //针对有参数的方法的调用 Method mm1 = class1.getMethod("sell", int.class); mm1.invoke(class1.newInstance(), 22); } }
反射设计工厂模式:
package com.training; public class test_3 { public static void main(String[] args) throws Exception { Factory.getInstance("com.training.Apple").eat(); } } interface Fruit{ public abstract void eat(); } class Apple implements Fruit{ public void eat() { // TODO Auto-generated method stub System.out.println("吃苹果"); } } class Orange implements Fruit{ @Override public void eat() { // TODO Auto-generated method stub System.out.println("吃橘子"); } } class Factory{ public static Fruit getInstance(String className) { Class<?> instance = null; try { instance=Class.forName(className); return (Fruit)instance.newInstance(); } catch (Exception e) { // TODO: handle exception return null; } } }