Java反射

java反射

1、涉及反射的一些方法
(1)getField getMethod getConstructor
getFields getMethods getConstructors
分别返回public域、方法和构造器数组,也包括其超类的共有成员
(2)getDeclareField getDeclareMethod getDeclaredConstructor
getDeclareFields getDeclareMethods getDeclaredConstructors
分别返回类中声明的全部域、方法和构造器数组,包括私有和受保护成员,但不包括超类的成员

public class Cat {
    public String name = "tomcat";
    public void hi(){
        System.out.println("hi " + name);
    }
}

①getField

Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Field field = cl.getField("name");
System.out.println(field.get(o));

在这里插入图片描述
②getMethod

Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Method method = cl.getMethod("hi");
method.invoke(o);

在这里插入图片描述
tip:对于调用静态方法时,不需要对象,在invoke()第一个参数设为 null

int s = 4;
Method me = Math.class.getMethod("sqrt", double.class);
System.out.println(me.invoke(null,s));

在这里插入图片描述

③getConstructor

Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Constructor constructor = cl.getConstructor();
System.out.println(constructor);

在这里插入图片描述
tip:
①加s形式时,则会获得多组数据,以数组形式返回

public class Cat {
    public String name = "tomcat";
    public int age = 10;
    public void hi(){
        System.out.println("hi " + name);
    }
}
Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Field[] field = cl.getFields();
System.out.println(field[0].get(o));
System.out.println(field[1].get(o));

在这里插入图片描述
②当类中成员变量为私有时,无法用getFiled获取,可使用getDeclaredField,并添加setAccessible();

Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Field field = cl.getDeclaredField("name");
field.setAccessible(true);
System.out.println(field.get(o));

在这里插入图片描述
③也可使用 类名+.class方式
例如:

Object o = com.reflection.Cat.class.newInstance();
Field field = com.reflection.Cat.class.getField("name");
System.out.println(field.get(o));

在这里插入图片描述

  • 一个例子
package com.xqsoft.modules.test;

import org.junit.Test;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.nio.channels.ScatteringByteChannel;

public class ReflectionTest {
    @Test
    public void method() throws Exception {
        String args ="";
//        Cat cat = new Cat();
//        cat.scram();
//        Class cl = Class.forName("com.xqsoft.modules.test.Cat");
//        Cat o = (Cat) cl.newInstance();
//        Field field = cl.getDeclaredField("age");
//        field.setAccessible(true);
//        field.set(o,"18");
//        System.out.println(o);
//        Cat cat = new Cat();
//        cat.setAge("12");
//        cat.setEye("brown");
//        cat.setNose("tall");
//        cat.setSex("NuM");
//        System.out.println(cat);

//        Cat cat = new Cat("12", "brown", "tall", "NuM");



        String[] attributeValue = {"12","brown","tall","NuM"};
        String[] attribute = {"age","eye","nose","sex"};

        Cat cat = utilsMethod("com.xqsoft.modules.test.Cat", attribute, attributeValue, Cat.class);
        System.out.println(cat);

        String[] attributeValue1 = {"12","brown"};
        String[] attribute1 = {"age","sex"};

        Dog dog = utilsMethod("com.xqsoft.modules.test.Dog", attribute1, attributeValue1, Dog.class);
        System.out.println(dog);


    }
    public <T> T utilsMethod (String args,String[] attribute,String[] attributeValue,Class<T> a) throws Exception{
        Class cl = Class.forName(args);

        T o = (T)cl.newInstance();
        Field[] fields = cl.getDeclaredFields();
        for(int i=0;i<fields.length;i++){
            Field field = cl.getDeclaredField(attribute[i]);
            field.setAccessible(true);
            field.set(o,attributeValue[i]);
        }
        return o;
    }
}
class Cat{
    private String age;
    private String sex;
    private String eye;
    private String  nose;
    public void scram(){
        System.out.println("I am scraming");
    }

    @Override
    public String toString() {
        return "Cat{" +
                "age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                ", eye='" + eye + '\'' +
                ", nose='" + nose + '\'' +
                '}';
    }

    public Cat() {
    }

    public String getAge() {
        return age;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getEye() {
        return eye;
    }

    public void setEye(String eye) {
        this.eye = eye;
    }

    public String getNose() {
        return nose;
    }

    public void setNose(String nose) {
        this.nose = nose;
    }
}
class Dog{
    private String age;
    private String sex;

    @Override
    public String toString() {
        return "Dog{" +
                "age='" + age + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值