Java中反射的三种常用方式

Java中反射的三种常用方式

package com.xiaohao.test;


public class Test{
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
// Class clazz=Class.forName("com.xiaohao.test.User"); //1方法一
// User user=(User) clazz.newInstance();

// User user=User.class.newInstance(); //2 方法二

User user2=new User(); //3 方法三
User user=user2.getClass().newInstance();
user.setId(10);
user.setUserName("小浩");
user.setPassword("123456");
System.out.println(user);

}
}

 

 

package com.xiaohao.test;

import java.util.ArrayList;
import java.util.List;

public class User {
private Integer id;
private String userName;
private String password;
List books=new ArrayList();



public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}

public User() {
super();
}

public List getBooks() {
return books;
}

public void setBooks(List books) {
this.books = books;
}

@Override
public String toString(){
return this.id+" "+this.userName+" "+this.password+" ";
}

 

}

 

 

 

 

 

 

package com.xiaohao.test;

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


public class Test{
public static void main(String[] args) throws Exception {
// Class clazz=Class.forName("com.xiaohao.test.User"); //1方法一
// User user=(User) clazz.newInstance();

// User user=User.class.newInstance(); //2 方法二

User user2=new User(); //3 方法三
User user=user2.getClass().newInstance();
System.out.println("user2对象的值为:"+user2);
System.out.println("类的名字为:"+user2.getClass().getName());
// Field field=user2.getClass().getDeclaredField("number");
// Field field=User.class.getDeclaredField("number");
Field field=Class.forName("com.xiaohao.test.User").getDeclaredField("number");
field.setAccessible(true);
field.set(user2,"1000");
System.out.println("user2对象的值为:"+user2);
Method method=User.class.getDeclaredMethod("setUserName",String.class);
method.invoke(user2,"小浩爷爷");
System.out.println("user2对象的值为:"+user2);
Class component=Class.forName("com.xiaohao.test.User").getDeclaredField("address").get(user2).getClass().getComponentType();
User.class.getDeclaredField("address").setAccessible(true);
int length=((String[])User.class.getDeclaredField("address").get(user2)).length;
System.out.println("user2中原始的数组的长度为:"+length);
Object [] array=(Object[]) Array.newInstance(component, length+75);
System.out.println("user2中修改后的数组的长度为:"+array.length);
user.setId(10);
user.setUserName("小浩");
user.setPassword("123456");
System.out.println(user);

}
}

 

 

 

 

package com.jd.singleton;

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

/**
 *
 *
 * Created by zhanghao10 on 2016/5/9.
 */
public class Reflect {
    /**
     *  测试反射生成对应的方法
     */
    public static void main (String args[]) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException, NoSuchMethodException {

//        Class clazz= Class.forName("com.jd.singleton.User");
//        User user= (User) clazz.newInstance();
//        User user2= (User) clazz.newInstance();
//        System.out.println(user==user2);
//        user.setName("Hello World");
//        System.out.println(user.equals(user2));
//        System.out.println(user.getName());
//        System.out.println(user2.getName());


//        Class clazz2=User.class;
//        User user=clazz2.newInstance();
//        user.setName("Hello World");
//        System.out.println(user.getName());


//        User user=new User();
//        User user1=user.getClass().newInstance();
//        user1.setName("Hello World");
//        System.out.println(user.getName());



//        Integer n1 = new Integer(47);
//        Integer n2 = new Integer(47);
//
//        System.out.println(n1 == n2);
//        System.out.println(n1.equals(n2));

//        Class clazz= Class.forName("com.jd.singleton.User");
//        Constructor[] cons=clazz.getConstructors();
//        System.out.println(cons.length);

//           Class clazz=Class.forName("com.jd.singleton.User");
//           Constructor [] cons= clazz.getDeclaredConstructors();
//           cons[0].setAccessible(true);//对于私有方法,需要设置可见性为false
//           User user= (User) cons[0].newInstance();
//           user.setName("123456");
//           System.out.println(user.getName());

//           Class clazz=Class.forName("com.jd.singleton.User");
//           Constructor [] cons= clazz.getDeclaredConstructors();
//           cons[0].setAccessible(true);//对于私有方法,需要设置可见性为false
//           User user= (User) cons[0].newInstance();
//           user.setName("123456");
//           System.out.println(user.getName());

//              Class clazz=Class.forName("com.jd.singleton.User");
//              User user= (User) clazz.newInstance();
//              Field filedName=clazz.getDeclaredField("name");
//              filedName.setAccessible(true);
//              filedName.set(user,"我是小浩也");
//              System.out.println(user.getName());

//                 Class clazz=Class.forName("com.jd.singleton.User");
//                 User user= (User) clazz.newInstance();
//                 Method method=clazz.getDeclaredMethod("setName",String.class);
//                 method.invoke(user,"天下太平");
//                System.out.println(user.getName());


                  Class clazz=Class.forName("com.jd.singleton.User");
                  User user= (User) clazz.newInstance();
                  Method method=clazz.getDeclaredMethod("setName",String.class,int.class);
                  method.invoke(user,"天下太平",123456);
                  System.out.println(user.getValue());








    }

}

  

 

package com.jd.singleton;

/**
 * Created by zhanghao10 on 2016/5/9.
 */
public class User {


//    private User(){}

    private String name;//用户名称
    private int value;

    public String getName() {
        return name;
    }

    public int getValue(){
        return value;
    }

    public void setName(String name,int value) {
        this.name = name;
        this.value=value;
    }

    @Override
    public int hashCode() {
        return (int) (Math.random()*100);
//        return super.hashCode();

    }

    @Override
    public boolean equals(Object obj) {
        return true;
//        return super.equals(obj);
    }
}

  

你可能感兴趣的:(Java中反射的三种常用方式)