java反射的一些东西

反射有关的api
java.lang包下中
Class<T>:表示一个正在运行的java应用程序中的类和接口
是Reflection的起源。
java.lang.reflect包下
Field类:代表类的成员变量也叫类的属性
Method类:代表类的方法
Constructor类:代表类的构造方法

//先创建一个Person类,我们将通过反射机制来得到该类的一些属性,方法,接口等等
public
class Person implements Serializable,ActionListener{ private static final long serialVersionUID = -1649734470743841201L; private String name; private int age; public String address; double salary; public Person() { } public Person(String name,int age)//前2种是共有的构造方法 { this.age=age; this.name=name; } Person(String name){ //默认的构造方法,不写权限为default this.name=name; } private Person(int age) //私有类型的构造方法 { this.age=age; } 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; } public String toString() { return "name:"+this.getName()+",age:"+this.getAge(); } public void sayhello() { System.out.println("hello world"); } private void smoke() { System.out.println("smoke is not good for person"); } @Override public void actionPerformed(ActionEvent e) { } }
//生成类对象常见2种方法
public
static void main(String[] args) { Class<?>c1=null; Class<?>c2=null; //使用对象的getClass() Person person=new Person(); c1=person.getClass(); //使用class.forname(),加载类对象,最常用 try { c2=Class.forName("Person"); } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println(c1.getName()); System.out.println(c2.getName()); } }
//反射获取构造方法有关的
public
class ConstructorDemo { public static void main(String[] args) { //反射构造方法 //先获取类对象 try { Class<?>c1=Class.forName("Person"); //获得公有的构造方法public System.out.println("所有共有的public构造方法为:"); Constructor []constructors=c1.getConstructors(); for(int i=0;i<constructors.length;i++) { System.out.println(constructors[i].toGenericString()); } //获得指定参数类型的共有的构造方法 System.out.println("指定参数类型的构造方法为:"); Constructor constructor1=c1.getConstructor(new Class[]{String.class,int.class}); System.out.println(constructor1.toGenericString()); //获得指定类型的共有的构造方法 System.out.println("指定类型的构造方法为:"); Constructor constructor2=c1.getDeclaredConstructor(new Class[]{int.class}); System.out.println(constructor2.toGenericString()); //获得所有类型构造方法 System.out.println("获得所有的构造方法为:"); Constructor []constructors3=c1.getDeclaredConstructors(); for(int i=0;i<constructors.length;i++) { System.out.println(constructors3[i].toGenericString()); } } catch (Exception e) { e.printStackTrace(); } } }
//通过反射机制的Field来获取方法相关的
/*
getFields()获得所有公有的属性
getDeclaredFields()获得所有的属性
getField(String name)获得指定名字的公有的属性
getDeclaredField(String name)获得指定名字的属性
*/

public
class FieldDemo { public static void main(String[] args) { Class<?>c=null; try { c=Class.forName("Person"); //获得所有公有的属性 System.out.println("打印所有公有的属性"); Field[]fields=c.getFields(); for(int i=0;i<fields.length;i++) { System.out.println(fields[i].toGenericString()); } //打印所有的属性 System.out.println("打印所有的属性"); fields=c.getDeclaredFields(); for(int i=0;i<fields.length;i++) { System.out.println(fields[i].toGenericString()); } //获得指定名字的共有的属性 Field f=c.getField("address"); System.out.println("获得指定元素的公有属性"); System.out.println(f.toGenericString()); //获得指定元素的属性 f=c.getDeclaredField("name"); System.out.println("获得指定元素的属性"); System.out.println(f.toGenericString()); } catch (Exception e) { e.printStackTrace(); } } }
//通过反射获取接口相关的一些信息
public
class InterfaceDemo { public static void main(String[] args) { //通过反射获得接口的信息 Class<?>c1=null; try { c1=Class.forName("Person"); System.out.println("打印接口的信息"); Class[] interfaces= c1.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { System.out.println(interfaces[i].toString()); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
//使用反射机制,动态实例化一个对象
public class DynamicInstanceDemo {

    public static void main(String[] args) {

        Class<?>c1=null;
        try {
            c1=Class.forName("Person");
            Constructor constructor=c1.getConstructor(new Class[]{String.class,int.class});
            Person person=(Person) constructor.newInstance("jack",23);
            System.out.println(person);//相当于默认加了tostring()方法
            //打印name:jack,age:23

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
public class ArrayDemo {

    public static void main(String[] args) {
        
        Person[] person=new Person[]{
            new Person("simant", 30),
            new Person("lucy", 22),
            new Person("linhong", 20)
        };
        System.out.println("数组的长度"+person.length);
        for (int i = 0; i <Array.getLength(person); i++) {
            System.out.println(Array.get(person, i));
            //遍历
        }
    }

}
原文地址:https://www.cnblogs.com/linhong/p/4221585.html