Java Reflect

获取类

TestClass.java

package org.example;

public class TestClass {}

Main.java

package org.example;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException {
//        System.out.println("Hello world!");
        Class c1 = TestClass.class;
        System.out.println(c1.getName());
        System.out.println(c1.getSimpleName());

        Class c2 = Class.forName("org.example.TestClass");
        System.out.println(c1 == c2);

        TestClass test = new TestClass();
        Class c3 = test.getClass();
        System.out.println(c3 == c2);
    }
}

获取类的构造器

TestClass.java

package org.example;

import org.junit.Test;

public class TestClass {
    private String name;
    private int age;

    public TestClass() {
    }

    public TestClass(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private TestClass(String name){
        this.name = name;
    }
}

Main.java

package org.example;

import org.junit.Test;

import java.lang.reflect.Constructor;

public class Main {
    @Test
    public void testGetConstructors() {
        Class c = TestClass.class;
//        Constructor[] constructors = c.getConstructors();  // 获取 public 构造器
        Constructor[] constructors = c.getDeclaredConstructors(); // 获取全部构造器
        for (Constructor constructor: constructors){
            System.out.println(constructor.getName() + "--->" + constructor.getParameterCount());
        }

    }

    @Test
    public void testGetConstructor() throws Exception {
        Class c = TestClass.class;
        Constructor constructor = c.getConstructor();  // 只能拿 public 无参构造器
        System.out.println(constructor.getName() + "--->" + constructor.getParameterCount());
        constructor = c.getDeclaredConstructor(); // 拿全部无参构造器
        System.out.println(constructor.getName() + "--->" + constructor.getParameterCount());
        constructor = c.getConstructor(String.class, int.class); // public
        System.out.println(constructor.getName() + "--->" + constructor.getParameterCount());
        constructor = c.getDeclaredConstructor(String.class);
        System.out.println(constructor.getName() + "--->" + constructor.getParameterCount());
    }
}

类构造器作用:初始化对象返回

TestClass.java

package org.example;

import org.junit.Test;

public class TestClass {
    private String name;
    private int age;

    public TestClass() {
    }

    public TestClass(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println(this.name + " " + this.age);
    }

    private TestClass(String name){
        this.name = name;
        System.out.println(this.name);
    }
}

Main.java

package org.example;

import org.junit.Test;

import java.lang.reflect.Constructor;

public class Main {
    @Test
    public void testGetConstructor() throws Exception {
        Class c = TestClass.class;
        Constructor constructor1 = c.getConstructor(String.class, int.class); // public
        System.out.println(constructor1.getName() + "--->" + constructor1.getParameterCount());
        TestClass test =(TestClass) constructor1.newInstance("test", 10);

        Constructor constructor2 = c.getDeclaredConstructor(String.class);  // private
        System.out.println(constructor1.getName() + "--->" + constructor1.getParameterCount());
        constructor2.setAccessible(true);
        TestClass test1 =(TestClass) constructor2.newInstance("test");
        test.setAge(10);
        test.setName("test");
        System.out.println(test1);
    }
}

获取类的成员变量

TestClass.java

package org.example;

import org.junit.Test;

public class TestClass {
    public static int o;
    public static final String test = "test-test";
    private String name;
    private int age;
}

Main.java

package org.example;

import org.junit.Test;

import java.lang.reflect.Field;

public class Main {
    @Test
    public void testGetField() throws Exception{
        // 获取类
        Class c = TestClass.class;
        // 获取所有成员变量
        Field[] fields = c.getDeclaredFields();
        for (Field field: fields){
            System.out.println(field.getName() + "--->" + field.getType());
        }
        // 获取某个成员变量
        Field fieldName = c.getDeclaredField("name");
        System.out.println(fieldName.getName() + "--->" + fieldName.getType());
        Field fieldAge = c.getDeclaredField("age");
        System.out.println(fieldAge.getName() + "--->" + fieldAge.getType());
        // 赋值
        TestClass test = new TestClass();
        fieldName.setAccessible(true);
        fieldName.set(test, "test");
        System.out.println(test);
        // 取值
        String name = (String) fieldName.get(test);
        System.out.println(name);
    }
}

获取类的成员方法

TestClass.java

package org.example;

import org.junit.Test;

public class TestClass {
    public static int o;
    public static final String test = "test-test";
    private String name;
    private int age;

    public TestClass() {
        System.out.println("无参构造...");
    }

    public TestClass(String name, int age) {
        this.name = name;
        this.age = age;
        System.out.println(this.name + " " + this.age);
    }

    private TestClass(String name){
        this.name = name;
        System.out.println(this.name);
    }

    private void sleep(){
        System.out.println("sleeping...");
    }

    private void sleep(String name){
        System.out.println(name + " sleeping...");
    }

    public void eat(String name){
        System.out.println(name + " eating");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Main.java

package org.example;

import org.junit.Test;

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

public class Main {
    @Test
    public void testGetField() throws Exception{
        // 获取类
        Class c = TestClass.class;
        // 获取所有成员方法
        Method[] methods = c.getDeclaredMethods();
        for (Method method: methods){
            System.out.println(method.getName() + "--->" + method.getParameterCount() + "--->" + method.getReturnType());
        }
        // 获取某个成员方法
        Method method1 = c.getDeclaredMethod("sleep", String.class);
        System.out.println(method1.getName() + "--->" + method1.getParameterCount() + "--->" + method1.getReturnType());
        Method method2 = c.getDeclaredMethod("sleep");
        System.out.println(method2.getName() + "--->" + method2.getParameterCount() + "--->" + method2.getReturnType());
        // 执行
        TestClass test = new TestClass();
        method2.setAccessible(true);
        method2.invoke(test);

    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇