反射调用方法时的两种情况,走get set和不走get set
@Test
public void test1() throws Exception{
//获取User类
Class class1=Class.forName("cn.jbit.bean.User");
//获取所有字段包括私有的
Field[] fileds=class1.getDeclaredFields();
for (Field field : fileds) {
System.out.println(field.getName());
}
//获取所有方法
Method[] methods = class1.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
//获取构造方法
Constructor[] constructors = class1.getDeclaredConstructors();
for (Constructor constructor : constructors) {
System.out.println("构造方法:"+constructor);
}
//调用所有方法
Method method = class1.getMethod("show");
Object obj=class1.newInstance();
//method.invoke(obj);
//调用set方法赋值
Field field = class1.getDeclaredField("name");
//设置为Accessible可进入的,因为name是自由字段
field.setAccessible(true);
//给私有字段赋值
field.set(obj,"sp");
method.invoke(obj);
}
//上面的给私有字段赋值的方法是不走get,set方法的,那么有时在get,set方法里进行判断就不管用了
//这时需要用PropertyDescriptor
@Test
public void test2() throws Exception{
//获取User类
Class class1=Class.forName("cn.jbit.bean.User");
//获取所有字段包括私有的
Field[] fileds=class1.getDeclaredFields();
for (Field field : fileds) {
System.out.println(field.getName());
}
//获取所有方法
Method[] methods = class1.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method.getName());
}
//调用所有方法
Method method = class1.getMethod("show");
Object obj=class1.newInstance();
//method.invoke(obj);
//调用set方法赋值
Field field = class1.getDeclaredField("name");
//属性描述
PropertyDescriptor pd=new PropertyDescriptor("name", class1);
//调用
Method method2 = pd.getWriteMethod();
method2.invoke(obj, "小红");
Method method3 = pd.getReadMethod();
String name = method3.invoke(obj).toString();
System.out.println(name);
}
反射调用方法时的两种情况,走get set和不走get set的更多相关文章
- 利用反射调用方法时,处理ref,out参数需要注意的问题(转)
转自:http://www.68idc.cn/help/buildlang/ask/20150318283817.html 项目中如下的泛型方法,因为要在运行时,动态指定类型参数,所以要利用反射来实现 ...
- java项目打jar包的两种情况
链接地址:http://jingyan.baidu.com/article/6b97984d8a6ddc1ca2b0bfa0.html 本文介绍一下java项目打jar包时的两种情况各怎么操作 方 ...
- WCF 客户端调用服务操作的两种方法
本节的主要内容:1.通过代理类的方式调用服务操作.2.通过通道的方式调用服务操作.3.代码下载 一.通过代理类的方式调用服务操作(两种方式添加代理类) 1.手动编写代理类,如下: 客户端契约: usi ...
- Hibernate多对多两种情况
Hibernate在做多对多映射的时候,除了原先的两张表外,会多出一个中间表做关联,根据中间表的会有两种不同的配置情况: 1.中间表不需要加入额外数据. 2.中间表有其他字段,需记录额外数据. 下面, ...
- JAVA反射调用方法
1.用户类 package com.lf.entity; import com.lf.annotation.SetProperty; import com.lf.annotation.SetTable ...
- 导致“mysql has gone away”的两种情况
导致“mysql has gone away”的两种情况 By Cruise 1. wait_timeout参数 在开发代理server时, 我使用了jdbc连接数据库,并采用长连接的方式连接数据库 ...
- 外壳exe通过反射调用dll时
外壳exe通过反射调用dll时,dll是 4.0的框架,外壳exe也需要编译成4.0的框架,如果dll本身有调用32位的dll,那么外壳exe也需要编译成32位. 调试时报的那个错,直接继续运行,不影 ...
- .NET/C# 反射的的性能数据,以及高性能开发建议(反射获取 Attribute 和反射调用方法)——转载
原文链接:https://blog.walterlv.com/post/dotnet-high-performance-reflection-suggestions.html ***** 大家都说反射 ...
- Day6------------磁盘用满的两种情况
1.文件包含元数据和写入的内容 元数据:存在硬盘中的inode ls -i /etc/passwd.bak 查看inode df -i 查看inode 2.磁盘用满的两种情况 1).内容太多 2).空 ...
随机推荐
- DataTable在内存中的使用
DataTable表示一个与内存有关的数据表,可以使用工具栏里面的控件拖放来创建和使用,也可以在编写程序过程中根据需要独立创建和使用,最常见的情况是作为DataSet的成员使用,在这种情况下就需要用在 ...
- 关于Android2.X系统自定义图片圆角BUG的解决
今天在做项目的时候遇到的一个问题. 预期的效果是这样的:
- Jquery广告浮动效果小案例
导入<script src="<%=path%>/html5/js/jquery.js"></script>文件 <SCRIPT type ...
- cat 显示指定行
[一]从第3000行开始,显示1000行.即显示3000~3999行 cat filename | tail -n +3000 | head -n 1000 [二]显示1000行到3000行 cat ...
- centos安装php
1.安装phpyum install php php-devel重启apache使php生效 2.此时可以在目录:/var/www/html/下建立一个PHP文件代码:<?php phpinfo ...
- zju(7)ADC操作实验
1.实验目的 1.学习和掌握S3C2410下ADC接口的操作方法以及应用程序的编写: 二.实验内容 1.编写EduKit-IV实验箱Linux操作系统下按键ADC的应用程序,并显示ADC的值. 三.主 ...
- 【转】Linux安装方法一(U盘引导)
Ubuntu 13.04正式版已经在4月25日发布了,相信很多人和我一样很想安装体验一下,但是现在的Ubuntu 13.04文件已经是794M,但是很难刻录到一张CD中,所以采用U盘启动安装Ubunt ...
- OSI七层与TCP/IP五层网络架构详解
引用自:http://www.2cto.com/net/201310/252965.html OSI和TCP/IP是很基础但又非常重要的网络基础知识,理解得透彻对运维工程师来说非常有帮助.今天 ...
- MVC应用程序中,怎样控制与复制相同的功能
先看此篇<MVC程序实现Autocomplete功能> http://www.cnblogs.com/insus/p/3546255.html 它是实现使用jQuery实现文本框输入文字, ...
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
http://blog.csdn.net/wave_1102/article/details/39271693 分类: Android(105) 作者同类文章X Android4.3 规范了BLE的A ...