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);
}
}

  

最新文章

  1. webgl巧妙方式写着色器代码
  2. 《Entity Framework 6 Recipes》中文翻译系列 (41) ------ 第七章 使用对象服务之标识关系中使用依赖实体与异步查询保存
  3. LeetCode---Binary Search
  4. C#可扩展编程之MEF学习笔记(二):MEF的导出(Export)和导入(Import)
  5. 安装debian第一天遇到的几个问题及解决方案
  6. hdu 4301(基本dp)
  7. Xcode7.1与iOS9之坑
  8. iOS View的Frame和bounds之区别,setbounds使用(深入探究)
  9. USACO6.4-The Primes
  10. 在Ubuntu下构建Bullet以及执行Bullet的样例程序
  11. testng xml 示例
  12. 关于JSP post请求乱码的问题
  13. git 仓库
  14. 为什么switch...case语句比if...else执行效率高
  15. php笔记一
  16. Ruby on Rails---Active Admin使用(一)
  17. ECharts 环形饼图 动态获取json数据
  18. java项目----衣服购买
  19. element-ui中上传文件upload
  20. 安利一个vps,7美元/年。

热门文章

  1. Java [Leetcode 102]Binary Tree Level Order Traversal
  2. 省常中模拟 Test4
  3. 一天一个Java基础——泛型
  4. 两个android程序间的相互调用(apk互调)
  5. 锋利的jQuery读书笔记---选择器
  6. MySQL基础之第1章 数据库概述
  7. 在PHP中如何获取用户的真实IP
  8. 在PC上收发短信--Pidgin短信(Linux Pidgin插件)
  9. C#中常见的winform控件命名规范
  10. HDU 5278 PowMod 数论公式推导