Java反射总结归纳
2018-06-17 本文已影响0人
海边的蜗牛ng
Java的反射很重要 作用有两个获取反射类型和反射调用(下面我会都说到,网上大多说的用法不全面)
- 获取Class
Class<Demo> c = Demo.class;//第一种方法
Class<?> c = Class.forName("package14_Demo");//第二种方式
code 准备 User.java
import java.io.Serializable;
/**
* Created by zhou on 18-1-16.
*/
abstract public class User implements Serializable {
public String name = "blake";
private int age = (int) StrictMath.round(StrictMath.random() * 1000);
private String address = (char) StrictMath.round(StrictMath.random() * 100000) + "" + (char) StrictMath.round(StrictMath.random() * 100000);
protected User(int age, String address) {
this.age = age;
this.address = address;
}
public User() {
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
- method的获取以及通过反射调用
public class Demo1 extends User{
public void info() {
System.out.println("info():" + "xxxxx ddd ggh");
}
protected void log() {
System.out.println("Is China's position on property market thawing?");
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, Exception {
{
Class<?> c = Class.forName("com.code14.c2.Demo1");
System.out.println("class name:" + c.getSimpleName());
/*获取所有的public修饰的方法 包括父类的方法*/
Method[] methods = c.getMethods();
for (Method m : methods) {
System.out.println("method = [" + m.getName() + "]");
}
/**
* 从打印结果看getMethods()获取了Object和User以及自身的所有被public修饰过的method
*/
}
System.out.println("-----------------------------------------||------------------------------------");
{
Class<?> c = Demo1.class;
/*获取class对象的所有声明方法:建议平时尽量使用getDeclaredMethods()*/
Method[] methods = c.getDeclaredMethods();
for (Method m : methods) {
System.out.println("method = [" + m.getName() + "]");
}
/**
* 从打印结果看:获取了自身所有定义过的所有方法
*/
}
System.out.println(".........................................||.....................................");
{
//当利用反射具体创建对象的时候必须要明确具体的类型
Class<Demo1> c = Demo1.class;
Demo1 demo = c.newInstance();
demo.info();
Method m = null;
try {
m = c.getMethod("log", null);
//由于方法没有参数,所以不用传入,假如有一个参数并且切好又是String,那么只需传入String.class即可
}catch (NoSuchMethodException e){
System.out.println("抛出异常:"+e.getMessage());
//抛出异常NoSuchMethodException 原来我的method用了 protected修饰
}
//具体使用
m = c.getDeclaredMethod("log", null);
m.invoke(demo, null);//传入的是对象而不是class
m = c.getMethod("getAddress",null);
System.out.println(m.invoke(demo, null));
m = c.getMethod("setAddress",String.class);
m.invoke(demo, "china");
m = c.getMethod("getAddress",null);
System.out.println(m.invoke(demo, null));
}
}
}
- field
public class Demo2 extends User {
private String account = (char) StrictMath.round(StrictMath.random() * 1000) + "" + (char) StrictMath.round(StrictMath.random() * 1000);
private double money = StrictMath.random() * 100;
public static void runB() throws Exception {
Class<?> demo = Class.forName("com.code14.c2.Demo2");
Field account = demo.getDeclaredField("account");
Field money = demo.getDeclaredField("money");
System.out.println("field Name:" + account.getName());
System.out.println("field Name:" + money.getName());
try {
Field f = demo.getDeclaredField("name");
System.out.println(f.getName() + "异常!不会打印");
} catch (Exception e) {
System.out.println("exception:" + e.getMessage());
}
/**
* 我们可以看到getDeclaredField()和getDeclaredFields()用法其实差不多的都是获取当前类的字段(当前类中任意修饰符都可以获取)
* 如果获取父类的字段会抛出异常
*/
System.out.println("----------------------------------||-------------------------");
//现在我们来看看如何获取field 上的value 具体使用
Demo2 demo2 = (Demo2) demo.newInstance();//get Demo2 Object
double m = (double) money.get(demo2);
String s = (String) account.get(demo2);
System.out.println("money value:" + m);
System.out.println("account value:" + s);
/**
* 能够正常的获取field上的value
*/
}
public static void runA() throws NoSuchFieldException, IllegalAccessException {
Class<Demo2> demo = Demo2.class;
Field name = demo.getField("name");
System.out.println("name:" + name);
Field age = demo.getField("age");//从这开始异常,因为后面4个全部是非public修饰过的
Field address = demo.getField("address");
Field account = demo.getField("account");
Field money = demo.getField("money");
System.out.println("age:" + age);
System.out.println("address:" + address);
System.out.println("account:" + account);
System.out.println("money:" + money);
/**
* getField()我们可以看到和getFields()相似都是获取public修饰过的字段并且都是子类和父类都可以获取
*/
}
public static void startA() throws Exception {
Class<?> demo = Class.forName("com.code14.c2.Demo2");
Field[] fields = demo.getFields();
for (Field f : fields) {
System.out.println("field Name:" + f.getName());
}
/**
* 打印结果:field Name:name
* 我们可以看到只有父类的User中的name字段被打印
* getFields()可以被确定为所有子类与父类的被public修饰过的字段
*/
}
public static void startB() throws Exception {
Class<Demo2> demo = Demo2.class;
Field[] fields = demo.getDeclaredFields();
for (Field f : fields) {
System.out.println("field Name:" + f.getName());
}
/**
* 打印结果:
* field Name:account
* field Name:money
* 如结果我们可以知道getDeclaredFields()获取当前类的所有定义的字段(修饰符可以是任意)
*/
}
public static void main(String[] args) throws Exception {
runB();
}
}
- Constructos
public class Demo3 extends User {
private String account = (char) StrictMath.round(StrictMath.random() * 1000) + "" + (char) StrictMath.round(StrictMath.random() * 1000);
private double money = StrictMath.random() * 100;
public static void main(String[] args) throws Exception {
runC();
}
/**
* 构造器的具体使用
*
* @throws Exception
*/
public static void runC() throws Exception {
Class<Demo3> demo3Class = Demo3.class;
Constructor<Demo3> demo3Constructor = demo3Class.getDeclaredConstructor(String.class, double.class);
Demo3 demo3 = demo3Constructor.newInstance("blake", StrictMath.round(StrictMath.random() * 100));
System.out.println("account:" + demo3.getAccount());
System.out.println("money:" + demo3.getMoney());
//Constructor没有参数那么就不需要传入参数
demo3Constructor = demo3Class.getDeclaredConstructor();
demo3 = demo3Constructor.newInstance();
System.out.println("account:" + demo3.getAccount());
System.out.println("money:" + demo3.getMoney());
}
public static void runB() throws Exception {
Class<?> demo3Class = Class.forName("com.code14.c2.Demo3", true, Demo3.class.getClassLoader());
//public Constructor<T> getConstructor(Class<?>... parameterTypes) jdk中的参数为可变数组
//public Demo3(String account, double money)打印结果如下:public com.code14.c2.Demo3(java.lang.String,double)
Constructor<?> c4 = demo3Class.getDeclaredConstructor(String.class, double.class);
/*public Demo3(int age, String address, String account, double money)
* 打印结果如下:public com.code14.c2.Demo3(int,java.lang.String,java.lang.String,double)*/
Constructor<?> c3 = demo3Class.getDeclaredConstructor(int.class, String.class, String.class, double.class);
//public Demo3(int age, String address) 打印结果:public com.code14.c2.Demo3(int,java.lang.String)
Constructor<?> c2 = demo3Class.getDeclaredConstructor(int.class, String.class);
//public Demo3() 打印结果如下com.code14.c2.Demo3()
Constructor<?> c1 = demo3Class.getDeclaredConstructor();
/**
* 从上面可以看到获取构造器的method getDeclaredConstructor()和getDeclaredConstructors()用法差不多
*同样需要注意的是它的参数顺序和参数类型的class数量
* getDeclaredConstructor()可以获取class的所有构造器但不包括父类的构造器
*/
}
public static void runA() throws Exception {
Class<?> demo3Class = Class.forName("com.code14.c2.Demo3", true, Demo3.class.getClassLoader());
//public Constructor<T> getConstructor(Class<?>... parameterTypes) jdk中的参数为可变数组
//public Demo3(String account, double money)打印结果如下:public com.code14.c2.Demo3(java.lang.String,double)
Constructor<?> c4 = demo3Class.getConstructor(String.class, double.class);
/*public Demo3(int age, String address, String account, double money)
* 打印结果如下:public com.code14.c2.Demo3(int,java.lang.String,java.lang.String,double)*/
Constructor<?> c3 = demo3Class.getConstructor(int.class, String.class, String.class, double.class);
//public Demo3(int age, String address) 打印结果:public com.code14.c2.Demo3(int,java.lang.String)
Constructor<?> c2 = demo3Class.getConstructor(int.class, String.class);
//Constructor<?> c1 = demo3Class.getConstructor();//c1 抛出异常,不能够获取因为是default修饰过的
/**
* 从上面可以看到获取构造器的method getConstructor()和getConstructors()用法基本差不多,只是getConstructor()
* 获取单个构造器,这个method要注意的是传入参数的class必须注意顺序和个数.很重要哦!
*/
}
public static void startA() throws Exception {
Class<?> demo3Class = Class.forName("com.code14.c2.Demo3", true, Demo3.class.getClassLoader());
Constructor<?>[] constructors = demo3Class.getConstructors();
for (Constructor<?> c : constructors) {
System.out.println("name:" + c.getName());
}
/**打印结果如下:
* name:com.code14.c2.Demo3
* name:com.code14.c2.Demo3
* name:com.code14.c2.Demo3
*/
/*我们可以看到getConstructors()只能够获取被public修饰的Constructor method
* 并且连父类被public修饰过的Constructor也不能够被获取这和Field以及Method很不一样*/
}
public static void startB() throws Exception {
Class<Demo3> demo3Class = Demo3.class;
Constructor<?>[] constructors = demo3Class.getDeclaredConstructors();
for (Constructor<?> c : constructors) {
System.out.println("name:" + c.getName());
}
/**
* name:com.code14.c2.Demo3
* name:com.code14.c2.Demo3
* name:com.code14.c2.Demo3
* name:com.code14.c2.Demo3
* 从结果上看到获取了当前类的四个构造器,依然没有获取超类的构造器
* 因此我们可以说getDeclaredConstructors()可以获取当前类所有的构造器(任何修饰符修饰过的都可以获取)
*/
}
public String getAccount() {
return account;
}
/**
* super Constructs
*
* @param age
* @param address
*/
public Demo3(int age, String address) {
super(age, address);
}
public Demo3(int age, String address, String account, double money) {
super(age, address);
this.account = account;
this.money = money;
}
public Demo3(String account, double money) {
this.account = account;
this.money = money;
}
Demo3() {
}
public double getMoney() {
return money;
}
}