Java反射Demo
2015-10-21 15:24
465 查看
代码结构:
1、用户Bean类:
package com.wc.plugin;
import java.io.Serializable;
/**
* @function 用户Bean类
* @author WRS
* @remark 用户Bean类
* @version 1.0
* @since jdk1.8
* @datetime 2015年10月4日 下午1:11:21
* @copyright {xx.com (c) 2018}
*/
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String userpwd;
public User() {
super();
}
public User(Long id, String username, String userpwd) {
this.id = id;
this.username = username;
this.userpwd = userpwd;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}
2、反射实现Demo:
package com.wc.plugin;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @function 反射Demo
* @author WRS
* @remark Java反射机制
* @version 1.0
* @since jdk1.8
* @datetime 2015年10月4日 下午1:10:10
* @copyright {xx.com (c) 2018}
*/
public class ReflectionMain {
public static void main(String[] args) throws NumberFormatException {
try {
Class<?> cls = Class.forName(User.class.getName());
User user = (User) cls.newInstance();
user.setId(new Long(2015));
user.setUsername("JAVA");
Method[] methods = cls.getMethods();//取得User的所有方法
for (Method item : methods) {
if("getId".equals(item.getName())){
Long getId = (Long) item.invoke(user);
System.out.println(getId);
}else if("getUsername".equals(item.getName())){
String getUsername = (String) item.invoke(user);
System.out.println(getUsername);
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
友情提示:本人提供相关IT技术开发和支持,与其相关技术交流。
如需请加微信号:
1、用户Bean类:
package com.wc.plugin;
import java.io.Serializable;
/**
* @function 用户Bean类
* @author WRS
* @remark 用户Bean类
* @version 1.0
* @since jdk1.8
* @datetime 2015年10月4日 下午1:11:21
* @copyright {xx.com (c) 2018}
*/
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private Long id;
private String username;
private String userpwd;
public User() {
super();
}
public User(Long id, String username, String userpwd) {
this.id = id;
this.username = username;
this.userpwd = userpwd;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
}
2、反射实现Demo:
package com.wc.plugin;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @function 反射Demo
* @author WRS
* @remark Java反射机制
* @version 1.0
* @since jdk1.8
* @datetime 2015年10月4日 下午1:10:10
* @copyright {xx.com (c) 2018}
*/
public class ReflectionMain {
public static void main(String[] args) throws NumberFormatException {
try {
Class<?> cls = Class.forName(User.class.getName());
User user = (User) cls.newInstance();
user.setId(new Long(2015));
user.setUsername("JAVA");
Method[] methods = cls.getMethods();//取得User的所有方法
for (Method item : methods) {
if("getId".equals(item.getName())){
Long getId = (Long) item.invoke(user);
System.out.println(getId);
}else if("getUsername".equals(item.getName())){
String getUsername = (String) item.invoke(user);
System.out.println(getUsername);
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
}
友情提示:本人提供相关IT技术开发和支持,与其相关技术交流。
如需请加微信号:
相关文章推荐
- Java反射随记
- AS3中的反射(速记) 分析
- C# 反射(Reflection)的用处分析
- C#通过反射创建自定义泛型
- 通俗易懂的C#之反射教程
- C#根据反射和特性实现ORM映射实例分析
- 深入反射生成数组的详解
- C#利用反射来判断对象是否包含某个属性的实现方法
- C#利用反射技术实现去掉按钮选中时的边框效果
- C#泛型和反射实例解析
- C# Dynamic关键字之:dynamic为什么比反射快的详解
- 传智播客学习之java 反射
- Java 反射之私有字段和方法详细介绍
- php使用反射插入对象示例分享
- java反射使用示例分享
- java利用反射实现动态代理示例
- 用户控件(ASCX)向网页(ASPX)传值使用反射实现
- 基于Java回顾之反射的使用分析
- c#反射调用方法示例
- python反射