Java 反射

2021-03-09 11:29

阅读:368

标签:htm   col   get   变量   nal   成员   color   成员变量   VID   

三种获取Class对象的方式
 
Person.java
package com.domain;

public class Person {

}

 

reflecDemo.java
package com.reflec;

import com.domain.Person;

public class reflecDemo {
    public static void main(String[] args) throws ClassNotFoundException {
        
        Class cls1 = Class.forName("com.domain.Person");
        System.out.println(cls1);
        
        
        Class cls2 = Person.class;
        System.out.println(cls2);
        
        Person p1 = new Person();
        Class cls3 = p1.getClass();
        System.out.println(cls3);
        
        System.out.println(cls1==cls2);
        System.out.println(cls2==cls3);
        
    }
}

 

运行结果:

class com.domain.Person
class com.domain.Person
class com.domain.Person
true
true

 

获取Field:成员变量
package com.domain;

public class Person {
    
    public String name;
    public String gender;
    private String pass;
    private String  captcha;
    protected String id;
    private final static String TEXT = "Hi reflecDemo";
    
    @Override
    public String toString() {
        return "Person [name=" + name + ", gender=" + gender + ", pass=" + pass
                + ", captcha=" + captcha + ", id=" + id + "]";
    }
}

 

package com.reflec;

import java.lang.reflect.Field;

import com.domain.Person;

public class reflecDemo2 {
    public static void main(String[] args) throws Exception {
        
        Class cls1 = Person.class;
        
        System.out.println("--------------public---------------");
        //获取字段,只能public公开字段
        Field name = cls1.getField("name");
        System.out.println(name.getName());
    
        //获取所有public字段
        Field[] fields = cls1.getFields();
        for(Field field : fields) {
            System.out.println(field.getName());
        }
        System.out.println("--------------public---------------");
        
        System.out.println("--------------private---------------");
        // 获取任意字段 
        Field pass = cls1.getDeclaredField("pass");
        System.out.println(pass.getName());
        
        //获取所有字段 
        Field[] fields2 = cls1.getDeclaredFields();
        for(Field field : fields2) {
            System.out.println(field.getName());
        }
        System.out.println("--------------private---------------");
        
        Field name2 = cls1.getDeclaredField("name");
        System.out.println(name2.getName());
        
        Person p1 = new Person();
        name2.set(p1, "s9mf");
        System.out.println(p1);
        
    }
}

 

--------------public---------------
name
name
gender
--------------public---------------
--------------private---------------
pass
name
gender
pass
captcha
id
TEXT
--------------private---------------
name
Person [name=s9mf, gender=null, pass=null, captcha=null, id=null]

 

Method:方法对象
 
package com.domain;

public class Person {

    public void getinfo(String work, int age) {
        System.out.println("毕业后求职方向:" + work + "   年龄:" + age );
    }
    
    private void gethobby() {
        System.out.println("play the GTA Online");
    }
}

 

package com;

import java.lang.reflect.Method;

import com.domain.Person;

public class reflecDemo {
    public static void main(String[] args) throws Exception {
        
        Person p1 = new Person();
        Class clazz = Person.class;
        
        //获取方法
        Method info = clazz.getMethod("getinfo", String.class, int.class);
        System.out.println("方法名:" + info.getName());
        info.invoke(p1, "光荣的打工人",20);
        
        //获取方法列表
        Method[] methods = clazz.getDeclaredMethods();
        for(Method method : methods) {
            System.out.println(method.getName());
        }
    }
}

 

方法名:getinfo
毕业后求职方向:光荣的打工人   年龄:20
getinfo
gethobby

 

package com.domain;

public class Person {
    
    public Person() {
    }
    
    public void gethobby() {
        System.out.println("play the GTA Online");
    }
}

 

package com;

import java.lang.reflect.Constructor;

import com.domain.Person;

public class reflecDemo {
    public static void main(String[] args) throws Exception {
        
        Class clazz = Class.forName("com.domain.Person");
        Constructor constructor1 = clazz.getConstructor();
        Person p1 = constructor1.newInstance();
        p1.gethobby();
        
    }
}

 

 

play the GTA Online

 

 

 https://www.cnblogs.com/adamjwh/p/9683705.html

https://www.bilibili.com/video/BV1nK411G7ur?p=9

https://note.youdao.com/ynoteshare1/index.html?id=128508bf6a04968eced81ede9ac1b304&type=note

Java 反射

标签:htm   col   get   变量   nal   成员   color   成员变量   VID   

原文地址:https://www.cnblogs.com/s0mf/p/14163696.html


评论


亲,登录后才可以留言!