5月16日 CMS 周三

package com.www;

public class Dog {
    public int did;
    public String dname;
    private String dhobby;
    public Dog() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Dog(int did, String dname, String dhobby) {
        super();
        this.did = did;
        this.dname = dname;
        this.dhobby = dhobby;
    }
    @Override
    public String toString() {
        return "Dog [did=" + did + ", dname=" + dname + ", dhobby=" + dhobby
                + "]";
    }
    public int getDid() {
        return did;
    }
    public void setDid(int did) {
        this.did = did;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    public String getDhobby() {
        return dhobby;
    }
    public void setDhobby(String dhobby) {
        this.dhobby = dhobby;
    }


}
package com.www;

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

public class Test {
    public static void main(String[] args) throws Exception {
        //三种方式
        Class c1 = Class.forName("com.www.Dog");
        Class c2 = Dog.class;
        Dog d = new Dog();
        Class c3 = d.getClass();

        System.out.println(c1.getPackage());
        System.out.println(c1.getName());
        System.out.println(c1.getSuperclass());
        System.out.println(c1.getInterfaces());

        Object obj = c1.newInstance();
        System.out.println(obj);

        Field[] fields = c1.getFields();
        Field[] declaredFields = c1.getDeclaredFields();

        Method[] methods = c1.getMethods();
        Method[] declaredMethods = c1.getDeclaredMethods();

        Constructor[] constructors = c1.getConstructors();
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();
    }
}

The man who fears losing has already lost.
怕输的人已经输了。
——乔治·马丁《权力的游戏》

猜你喜欢

转载自blog.csdn.net/helloworld_1996/article/details/80344774