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

  

Java中反射的三种常用方式的更多相关文章

  1. Java中反射的实现方式

    所谓反射,是指在运行时状态中,获取类中的属性和方法,以及调用其中的方法的一种机制.这种机制的作用在于获取运行时才知道的类(Class)及其中的属性(Field).方法(Method)以及调用其中的方法 ...

  2. Java中创建对象的几种方式

    Java中创建对象的五种方式: 作为java开发者,我们每天创建很多对象,但是我们通常使用依赖注入的方式管理系统,比如:Spring去创建对象,然而这里有很多创建对象的方法:使用New关键字.使用Cl ...

  3. java中反射学习整理

    转载请注明:http://blog.csdn.net/j903829182/article/details/38405735 反射主要是指程序能够訪问.检測和改动它本身的状态或行为的一种能力. jav ...

  4. Java中创建对象的五种方式

    我们总是讨论没有对象就去new一个对象,创建对象的方式在我这里变成了根深蒂固的new方式创建,但是其实创建对象的方式还是有很多种的,不单单有new方式创建对象,还有使用反射机制创建对象,使用clone ...

  5. 【转】Java中创建对象的5种方式

    Java中创建对象的5种方式   作为Java开发者,我们每天创建很多对象,但我们通常使用依赖管理系统,比如Spring去创建对象.然而这里有很多创建对象的方法,我们会在这篇文章中学到. Java中有 ...

  6. Java进阶(四十二)Java中多线程使用匿名内部类的方式进行创建3种方式

    Java中多线程使用匿名内部类的方式进行创建3种方式 package cn.edu.ujn.demo; // 匿名内部类的格式: public class ThreadDemo { public st ...

  7. Java中的静态代理实现方式

    1.编写一个接口类 如:Subject package com.neusoft.pattern.staticProxy; /** * <p>Title:</p> * <p ...

  8. JAVA中循环遍历list有三种方式

    转自:https://blog.csdn.net/changjizhi1212/article/details/81036509JAVA中循环遍历list有三种方式for循环.增强for循环(也就是常 ...

  9. Java中正负数的存储方式-正码 反码和补码

    Java中正负数的存储方式-正码 反码和补码 正码 我们以int 为例,一个int占用4个byte,32bits 0 存在内存上为 00000000 00000000 00000000 0000000 ...

随机推荐

  1. Java [Leetcode 169]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

  2. 【Python】实践笔记

    为什么要在脚本中加入? import sys reload(sys) sys.setdefaultencoding('utf-8')

  3. Blog CSS

    你好 print("你好.") haode

  4. [Everyday Mathematics]20150119

    设 $V$ 是 $n$ 维线性空间, $V_1, V_2$ 均为 $V$ 的子空间, 且 $$\bex V_1\subset V_2,\quad \dim V=10,\quad \dim V_1=3, ...

  5. hadoop2.0中无法启动datanode的问题

    问题描述:在启动datanode进程时,能成功的启动:但用jps查看进程时,发现进程不存在,下面是在datanode日记文件的错误信息 如下图的截屏所示: 主要原因:发生错误的原因:由于把data放在 ...

  6. java中的getClass()函数

    Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...

  7. java web 学习四(http协议)

    一.什么是HTTP协议 HTTP是hypertext transfer protocol(超文本传输协议)的简写,它是TCP/IP协议的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...

  8. sql test

    1.用户表 找出id=2及他的朋友 select * from user t where id=2 or t.id=(select friend from user where id=2); sele ...

  9. 【C++对象模型】构造函数语意学之二 拷贝构造函数

    关于默认拷贝构造函数,有一点和默认构造函数类似,就是编译器只有在[需要的时候]才去合成默认的拷贝构造函数. 在什么时候才是[需要的时候]呢? 也就是类不展现[bitwise copy semantic ...

  10. Ubuntu 12.04中文输入法的安装

    Ubuntu 12.04中文输入法的安装   Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu ...