首页 > 技术文章 > 通过类对象来获取类中的属性,方法,构造器

li33 2020-04-20 21:36 原文

获取类的信息(实际开发用的很少,但是要知道)

通过Class对象来获取类中的变量,方法,构造器,属性等。其中包括私有个公有!

package Reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test06 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c1 =  Class.forName ("Reflection.User");
//        User user = new User ();
//        c1 =user.getClass ();
        System.out.println (c1.getName ());//获得包名加类名
        System.out.println (c1.getSimpleName ());//获取类名
        Field[] fields = c1.getFields ();
        for (Field field : fields) {//只能打印public方法
            System.out.println (field);
        }
        fields = c1.getDeclaredFields ();//可以强制获取所以方法,包括私有
        for (Field field : fields) {
            System.out.println (field);
        }
//        Field name = c1.getField("name");//获取指定属性的值
//        System.out.println (name);
        Field name1 = c1.getDeclaredField ("name");//获取指定属性的值
        System.out.println (name1);
//获取本类的全部方法,包括其父类的全部public方法。
        Method[] methods =c1.getMethods();
        for (Method method : methods) {
            System.out.println ("正常的方法:"+method);
        }
        //获取全部的方法,包括私有
        methods = c1.getDeclaredMethods ();
        for (Method method : methods) {
            System.out.println ("全部的方法:"+method);
        }
        Method getName = c1.getMethod ("getName",null);
        Method setName = c1.getMethod ("setName",String.class);//传入的参数类型
    //获取不同类型构造器的两种方式
        Constructor[] constructors = c1.getConstructors ();
        for (Constructor constructor : constructors) {
            System.out.println (constructor);
        }
        constructors = c1.getDeclaredConstructors ();
        for (Constructor constructor : constructors) {
            System.out.println ("#"+constructor);
        }
        Constructor declaredConstructor =  c1.getDeclaredConstructor (String.class,int.class,int.class);
        System.out.println ("指定:"+declaredConstructor);
    }
}
运行结果:
Reflection.User
User
private java.lang.String Reflection.User.name
private int Reflection.User.id
private int Reflection.User.age
private java.lang.String Reflection.User.name
正常的方法:public java.lang.String Reflection.User.getName()
正常的方法:public int Reflection.User.getId()
正常的方法:public void Reflection.User.setName(java.lang.String)
正常的方法:public void Reflection.User.setId(int)
正常的方法:public int Reflection.User.getAge()
正常的方法:public void Reflection.User.setAge(int)
正常的方法:public final void java.lang.Object.wait() throws java.lang.InterruptedException
正常的方法:public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
正常的方法:public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
正常的方法:public boolean java.lang.Object.equals(java.lang.Object)
正常的方法:public java.lang.String java.lang.Object.toString()
正常的方法:public native int java.lang.Object.hashCode()
正常的方法:public final native java.lang.Class java.lang.Object.getClass()
正常的方法:public final native void java.lang.Object.notify()
正常的方法:public final native void java.lang.Object.notifyAll()
全部的方法:public java.lang.String Reflection.User.getName()
全部的方法:public int Reflection.User.getId()
全部的方法:public void Reflection.User.setName(java.lang.String)
全部的方法:public void Reflection.User.setId(int)
全部的方法:public int Reflection.User.getAge()
全部的方法:public void Reflection.User.setAge(int)
public Reflection.User()
public Reflection.User(java.lang.String,int,int)
#public Reflection.User()
#public Reflection.User(java.lang.String,int,int)
指定:public Reflection.User(java.lang.String,int,int)

推荐阅读