1、

package reflectionZ;

import java.lang.reflect.Field;
import java.lang.reflect.Method; public class Treflection04
{
// 第17讲 public static void main(String[] args) throws Exception
{
Test2 test2 = new Test2();
//int iRtn = Sum01(test2);
int iRtn = Sum02(test2);
System.out.println("iRtn : "+iRtn);
} public static int Sum01(Test2 _test2) throws Exception
{
int iRst = 0;
Class<?> clazz1 = Class.forName("reflectionZ.Test2");
Field[] fields = clazz1.getDeclaredFields();
for (int i=0; i<fields.length; i++)
{
Field field = fields[i];
System.out.println("field.getGenericType() : "+field.getGenericType());
//if (int.class == field.getType()) // 方法1
if (field.getGenericType().toString().equalsIgnoreCase("int")) // 方法2
{
field.setAccessible(true);
int j = field.getInt(_test2);
iRst += j;
}
} return iRst;
} public static int Sum02(Test2 _test2) throws Exception
{
int iRst = 0;
Class<?> clazz1 = Class.forName("reflectionZ.Test2");
Method[] methods = clazz1.getMethods();
for (int i=0; i<methods.length; i++)
{
Method method = methods[i];
if (method.getName().startsWith("get") && (! method.getName().equalsIgnoreCase("getClass"))) // 排除掉 getClass()
{
// 函数 有返回值,处理方式
//int j = (int)method.invoke(_test2); // ZC: 这种处理方式 ==> 行不通
Integer j = (Integer)method.invoke(_test2);
iRst += j;
}
} return iRst;
}
} class Test2
{
public static final Long serverUID = 1L; private int FiIdx1 = 10;
private int FiIdx2 = 20;
private int FiIdx3 = 30;
private int FiIdx4 = 40;
private int FiIdx5 = 50; public int getFiIdx1() {
return FiIdx1;
}
public int getFiIdx2() {
return FiIdx2;
}
public int getFiIdx3() {
return FiIdx3;
}
public int getFiIdx4() {
return FiIdx4;
}
public int getFiIdx5() {
return FiIdx5;
}
}

2、

最新文章

  1. 不使用session,借助redis实现验证码
  2. hosts 文件妙用
  3. fatal: Not a valid object name: &#39;master&#39;.
  4. 使用花生壳6.5客户端FTP设置
  5. JavaScript Patterns 4.7 Init-Time Branching
  6. 提升效率的Linux终端快捷操作汇总
  7. (Data structure)Implement Trie &amp;&amp; Add and Search Word
  8. SQL Server数据恢复——日志备份
  9. compass安装教程
  10. iOS中NSString转换成HEX(十六进制)-NSData转换成int
  11. ContentProvider简要
  12. krpano漫游加方向性3D声音(这篇文章已被移到krpano中国网站 krpano360.com)
  13. Javscript轮播 支持平滑和渐隐两种效果(可以只有两张图)
  14. 设计模式学习(三): 装饰者模式 (附C#实现)
  15. 【BSGS】BZOJ3239 Discrete Logging
  16. Java修饰符final总结
  17. 微信小程序记账本进度三
  18. 【HAOI 2012】高速公路
  19. git使用经验for windows
  20. linux http配置

热门文章

  1. 短URL DH 密钥交换算法
  2. 原生js实现ajax方法
  3. python setup.py install 报错:error: [WinError 3] 系统找不到指定的路径。: &#39;C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\PlatformSDK\\lib
  4. json的相关操作
  5. C/C++运算符及其优先级
  6. 007-shiro与spring web项目整合【一】基础搭建
  7. C#设置当前程序通过IE代理服务器上网
  8. 吴超老师课程--Pig的介绍和安装
  9. Codeforces Round #396 (Div. 2) A - Mahmoud and Longest Uncommon Subsequence B - Mahmoud and a Triangle
  10. Hive2.2.1概述(待重写)