反射

加粗樣式
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

@TestAnnotation(value1="屬性1", value2=10)
public class Person {

    private String name ;
    private int age;

    public Person() {
        System.out.println("我愛你");
    }

    public void say(){
        System.out.println("jacky");
    }

    public void eat() {
        System.out.println("我在吃");
    }
}

import annotation.Person;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class TestRefluction {

public static void main(String[] args) throws Exception {
    Class personClass1 = new annotation.Person().getClass();
    Class personClass2 = annotation.Person.class;
    Class personClass3 = Class.forName("annotations.Person");

    //1. 實例化一個對象
    annotation.Person person = (Person)personClass3.newInstance();

    //2. 調用方法
    Method method = personClass3.getDeclaredMethod("say");
    method.invoke(person);

    Method[] methods = personClass3.getDeclaredMethods();
    for (Method method1 : methods) {
        method1.invoke(person);
    }

        //調用私有方法
//        Method methodEat = personClass3.getDeclaredMethod("eat");
//        methodEat.setAccessible(true);
//        methodEat.invoke(person);

    //3.獲取屬性
    Field fieldAge = personClass3.getDeclaredField("age");
    fieldAge.setAccessible(true);
    fieldAge.set(person, 5);
    System.out.println(fieldAge.get(person));

    //4.獲取註解
    System.out.println(personClass3.isAnnotationPresent(TestAnnotation.class));
    TestAnnotation annotation = (TestAnnotation) personClass3.getAnnotation(TestAnnotation.class);
    System.out.println(annotation.value1());
    System.out.println(annotation.value2());
}
}


**> 通過反射調用私有屬性**

import lesson.animal.moder.Teacher;

import java.lang.reflect.Field;

/**
 * User: 彭家琪
 * Date: 2019/9/11   17:33
 */
public class DemoTongYong {
    public static void main(String[] args) throws Exception {

//調用Tool
        Teacher teacher = new Teacher();
        Tool.setFiledValue(teacher,"name","jq");
        Tool.setFiledValue(teacher,"homntown","nc");
        Tool.setFiledValue(teacher,"height",1.7);
        Tool.setFiledValue(teacher,"color","white");

        System.out.println(teacher);


    }

    private static void test() throws Exception {

        //傳統通過反射的調用私有屬性
        Teacher teacher = new Teacher();
        //獲取字節碼對象
        Class clz = Teacher.class;

        //獲取屬性值
        Field name = clz.getDeclaredField("name");
        Field homntown = clz.getDeclaredField("homntown");
        Field height = clz.getDeclaredField("height");

        //設置權限
        name.setAccessible(true);
        homntown.setAccessible(true);
        height.setAccessible(true);

        //給屬性賦值
        name.set(teacher,"jq");
        homntown.set(teacher,"nc");
        height.set(teacher,1.7);
        System.out.println(teacher);
    }
}
class Tool{
    public static void setFiledValue(Object obj,String filedName,Object filedValue) throws Exception{
        //獲取字節碼對象
        Class clz = Teacher.class;

        //獲取屬性值
        Field declaredField = clz.getDeclaredField(filedName);

        //設置權限
        declaredField.setAccessible(true);

        //給屬性賦值
        declaredField.set(obj,filedValue);
    }
    }

public class Teacher {
			private String name;
			private String homntown;
			private double height;
			public String color;

public Teacher() {
	super();
	// TODO Auto-generated constructor stub
}
public Teacher(String name, String homntown) {
	super();
	this.name = name;
	this.homntown = homntown;
}
public Teacher(String name, double height) {
	super();
	this.name = name;
	this.height = height;
}
@Override
public String toString() {
	return "Teacher [name=" + name + ", homntown=" + homntown + ", height=" + height + ", color=" + color + "]";
}
}

通過反射調用私有方法


public class Teacher {

	private String name;
	private String homntown;
	private double height;
	public String color;
	
	public Teacher() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Teacher(String name, String homntown) {
		super();
		this.name = name;
		this.homntown = homntown;
	}
	public Teacher(String name, double height) {
		super();
		this.name = name;
		this.height = height;
	}
	@Override
	public String toString() {
		return "Teacher [name=" + name + ", homntown=" + homntown + ", height=" + height + ", color=" + color + "]";
	}
	
	/*public void say1(){
		System.out.println("我是一隻小黃雞...");
	}
	
	public void say2(String name){
		System.out.println("我是一隻" + name);
	}
	
	public void say3(String name,int age){
		System.out.println("我是一隻" + name + " 今年" + age);
	}*/
	
	private void say1(){
		System.out.println("我是一隻小黃雞...");
	}
	
	private void say2(String name){
		System.out.println("我是一隻" + name);
	}
	
	private void say3(String name,int age){
		System.out.println("我是一隻" + name + " 今年" + age);
	}

}



import java.lang.reflect.Method;

import com.gyf.model.Teacher;

public class Demo01 {

public static void main(String[] args) throws Exception{
	//通過反射獲取方法並使用
	/**
	 * 1.反射中通過Method類描述方法【構造方法:Contructor,字段:Field】
	   2.通過Class的getMethod可以獲取一個方法
	   3.通過getDeclaredMethod可以獲取私有方法
	   4.如果要調用私有方法,設置訪問權限setAccessible
	 */
	
	//1.獲取字節碼對象
	Class clz = Teacher.class;
			
	Teacher teacher = new Teacher();
/*	teacher.say1();
	teacher.say2("小黃鴨");
	teacher.say3("小黃鴨",2);*/
	
	//2.通過反射調用方法
	//2.1 獲取無參方法
	Method m1 = clz.getDeclaredMethod("say1");
	//2.1 獲取有參方法
	Method m2 = clz.getDeclaredMethod("say2", String.class);
	Method m3 = clz.getDeclaredMethod("say3", String.class,int.class);

	//設置私有方法可以訪問
	m1.setAccessible(true);
	m2.setAccessible(true);
	m3.setAccessible(true);
	//3.2 調用方法
	m1.invoke(teacher);
	m2.invoke(teacher, "小豬");
	m3.invoke(teacher, "小狗",98);
}
}

`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章