Java反射基礎

來源:互聯網
上載者:User

標籤:

import java.io.InputStream;import java.util.List;public class Person {    public Integer id = 1;    private String name = "default";    private static String password = "pass";    public Person() {        System.out.println("無參建構函式");    }    public Person(Integer id, String name) {        this.id = id;        this.name = name;        System.out.println("有參建構函式");    }    private Person(List list) {        System.out.println("私人有參建構函式");    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String run() {        System.out.println("run...");        return "run";    }    public String eat() {        System.out.println("eat...");        return "eat";    }    /*測試反射的方法*/    public void m1() {        System.out.println("m1");    }    public void m1(String name, int age) {        System.out.println(name + " - " + age);    }    public Class[] m1(String name, int[] age) {        return new Class[]{String.class};    }    private void m1(InputStream inputStream) {        System.out.println(inputStream);    }    public static void m1(int num) {        System.out.println(num);    }    public static void main(String[] args) {        System.out.println("main method");    }}
import java.io.InputStream;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;public class ReflectDemo {    public static void main(String[] args) throws InvocationTargetException {        try {            Class clazz = Class.forName("Person");            // 反射構造器            Constructor constructor = clazz.getConstructor(null);            Person person = (Person) constructor.newInstance(null);            Constructor constructor1 = clazz.getConstructor(Integer.class, String.class);            Person person1 = (Person) constructor1.newInstance(1, "jack");            Constructor constructor2 = clazz.getDeclaredConstructor(List.class);// private method            constructor2.setAccessible(true);            Person person2 = (Person) constructor2.newInstance(new ArrayList<>());            Person person3 = (Person) clazz.newInstance();            // 反射類的方法            Person p = new Person();            Method method = clazz.getMethod("m1", null);            method.invoke(p, null);            Method method1 = clazz.getMethod("m1", String.class, int.class);            method1.invoke(p, "jack", 11);            Method method2 = clazz.getMethod("m1", String.class, int[].class);            Class[] classes = (Class[]) method2.invoke(p, "rose", new int[]{1, 2, 3});            System.out.println(classes[0]);            Method method3 = clazz.getDeclaredMethod("m1", InputStream.class);            method3.setAccessible(true);            /*method3.invoke(p, null);*/            Method method4 = clazz.getMethod("m1", int.class);            method4.invoke(null, 23); //靜態方法調用不需要對象            // 反射 main 方法            Method main = clazz.getMethod("main", String[].class);            //main.invoke(null, new Object[]{new String[]{"a", "b"}});            main.invoke(null, (Object) new String[]{"a", "b"});            // 反射類的欄位            Field field = clazz.getField("id");            Class type = field.getType();            if (type.equals(Integer.class)) {                field.set(p, 11);// setter                Integer id = (Integer) field.get(p);                System.out.println(id);            }            Field field1 = clazz.getDeclaredField("name");            field1.setAccessible(true);            String name = (String) field1.get(p);            System.out.println(name);            Field field2 = clazz.getDeclaredField("password");            field2.setAccessible(true);            String password = (String) field2.get(p);            System.out.println(password);        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | NoSuchFieldException e) {            e.printStackTrace();        }    }}

 

可以用到的工具類:BeanUtils,http://commons.apache.org/proper/commons-beanutils/

Java反射基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.