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<String> books=new ArrayList<String>();



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<String> getBooks() {
return books;
}

public void setBooks(List<String> 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<User> 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);
    }
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值