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、

Treflection04_面试题的更多相关文章

  1. .NET面试题系列[8] - 泛型

    “可变性是以一种类型安全的方式,将一个对象作为另一个对象来使用.“ - Jon Skeet .NET面试题系列目录 .NET面试题系列[1] - .NET框架基础知识(1) .NET面试题系列[2] ...

  2. 关于面试题 Array.indexof() 方法的实现及思考

    这是我在面试大公司时碰到的一个笔试题,当时自己云里雾里的胡写了一番,回头也曾思考过,最终没实现也就不了了之了. 昨天看到有网友说面试中也碰到过这个问题,我就重新思考了这个问题的实现方法. 对于想进大公 ...

  3. 对Thoughtworks的有趣笔试题实践

    记得2014年在网上看到Thoughtworks的一道笔试题,当时觉得挺有意思,但是没动手去写.这几天又在网上看到了,于是我抽了一点时间写了下,我把程序运行的结果跟网上的答案对了一下,应该是对的,但是 ...

  4. 从阿里巴巴笔试题看Java加载顺序

    一.阿里巴巴笔试题: public class T implements Cloneable { public static int k = 0; public static T t1 = new T ...

  5. JAVA面试题

    在这里我将收录我面试过程中遇到的一些好玩的面试题目 第一个面试题:ABC问题,有三个线程,工作的内容分别是打印出"A""B""C",需要做的 ...

  6. C++常考面试题汇总

    c++面试题 一 用简洁的语言描述 c++ 在 c 语言的基础上开发的一种面向对象编程的语言: 应用广泛: 支持多种编程范式,面向对象编程,泛型编程,和过程化编程:广泛应用于系统开发,引擎开发:支持类 ...

  7. .NET面试题系列[4] - C# 基础知识(2)

    2 类型转换 面试出现频率:主要考察装箱和拆箱.对于有笔试题的场合也可能会考一些基本的类型转换是否合法. 重要程度:10/10 CLR最重要的特性之一就是类型安全性.在运行时,CLR总是知道一个对象是 ...

  8. 我们公司的ASP.NET 笔试题,你觉得难度如何

    本套试题共8个题,主要考察C#面向对象基础,SQL和ASP.NET MVC基础知识. 第1-3题会使用到一个枚举类,其定义如下: public enum QuestionType { Text = , ...

  9. 我设计的ASP.NET笔试题,你会多少呢

    本笔试题考查范围包括面向对象基础.HTML.CSS.JS.EF.jQuery.SQL.编码思想.算法等范围. 第1题:接口和抽象类有何区别? 第2题:静态方法和实例方法有何区别? 第3题:什么是多态? ...

随机推荐

  1. 『浅入深出』MySQL 中事务的实现

    在关系型数据库中,事务的重要性不言而喻,只要对数据库稍有了解的人都知道事务具有 ACID 四个基本属性,而我们不知道的可能就是数据库是如何实现这四个属性的:在这篇文章中,我们将对事务的实现进行分析,尝 ...

  2. Oracle Schema Objects——Tables——Overview of Tables

    Oracle Schema Objects Overview of Tables A table is the basic unit of data organization in an Oracle ...

  3. php and mysql pear的安装

    http://www.cnblogs.com/bugY/archive/2012/07/06/2578972.html 什么是PEAR 来自百度百科:PEAR是PHP扩展与应用库(the PHP Ex ...

  4. 服务器端Session和客户端Session(和Cookie区别)

    Session其实分为客户端Session和服务器端Session. 当用户首次与Web服务器建立连接的时候,服务器会给用户分发一个 SessionID作为标识.SessionID是一个由24个字符组 ...

  5. Qt 使用 lambda 表达式做为槽函数时为什么使用 QObject::sender() 获取到的发送信号对象指针为空?

    /*! Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; ...

  6. 事件对象event之e.targtet || e.srcElement

    p.onclick = function (event) { var e = event || window.event, target = e.target ? e.target : e.srcEl ...

  7. java中集合的扩容

    对于Java中的各种集合类,根据底层的具体实现,小结了一下大致有3种扩容的方式: 1.对于以散列表为底层数据结构实现的,(譬如hashset,hashmap,hashtable等),扩容方式为当链表数 ...

  8. Kotlin开发Android笔记

    外国人写的一个天气预报的例子,最后有源码下载地址,初学者可以研读一下 http://blog.csdn.net/true100/article/category/6257988 1:Kotlin介绍及 ...

  9. php imagemagick库安装使用

    imagemagick介绍: ImageMagick® is a software suite to create, edit, compose, or convert bitmap images. ...

  10. iOS学习之七牛云存储应用

    前言 七牛云存储,是专为移动时代开发者打造的数据管理平台,为互联网网站和移动App提供数据的在线托管.传输加速以及图片.音视频等富媒体的云处理服务. 七牛云官网http://www.qiniu.com ...