博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【代码积累】reflection study
阅读量:4098 次
发布时间:2019-05-25

本文共 2496 字,大约阅读时间需要 8 分钟。

import java.lang.reflect.*;import java.util.Map;public class Test {	public String name = null;	private int cnt = 1;		public void setCnt(int cnt)	{		this.cnt = cnt;	}		public Test()	{			}		public Test(String name)	{		this.name = name;	}		public void test()	{		Method[] method = Test.class.getMethods();		for( Method item:method )		{			System.out.println("Method in test: "+item.getName());		}				/*Get fields*/		Field[] fields = Test.class.getFields();		for( Field field:fields )		{			System.out.println("field name ="+field.getName()); /*Only private fields can be accessed*/		}				/*Modify field value*/		try		{			Field namefield = Test.class.getField("name");						Test newtest = new Test();			newtest.name = new String("hahaha");						String value = (String)namefield.get(newtest); /*Use the Field object to get value of this field belongs to the specific object*/			System.out.println("value ="+value);						/*Now modify the field*/			value = new String("heiheihei");			namefield.set(newtest, value);			System.out.println("new value="+newtest.name);		}		catch(Exception e)		{			e.printStackTrace();		}				/*Get all constructors*/		Constructor
[] constructors = Test.class.getConstructors(); for( Constructor cons:constructors ) { System.out.println("Constructor name="+cons.getName()); } /*Get the constructor that takes a String as argument*/ try { /*usr constructor to instantiate an object*/ Constructor cons = Test.class.getConstructor(String.class); Test test1 = (Test)cons.newInstance("CreatedViaConstructorClass"); System.out.println("Name = "+test1.name); /*Get class name*/ System.out.println("modifiers: "+ Test.class.getModifiers()); System.out.println("name: "+Map.class.getName()); System.out.println("simple-name: "+Map.class.getSimpleName()); System.out.println(""); /*Get method,use the method object to invoke the method*/ Method methodlocal = Test.class.getMethod("test", null); Test test2 = new Test(); System.out.println("++++++++++\r\n"); //Object returnvalue = methodlocal.invoke(test2, null); /*@Get privite field*/ Field fieldint = Test.class.getDeclaredField("cnt"); /*getField can only get public fields*/ fieldint.setAccessible(true); Test test3 = new Test(); test3.setCnt(12); int cntvalue = fieldint.getInt(test3); System.out.println("private field cnt ="+cntvalue); } catch(Exception e) { e.printStackTrace(); } } public String classname() { return Test.class.getSimpleName(); }}

转载地址:http://nthii.baihongyu.com/

你可能感兴趣的文章
XHProf-php轻量级的性能分析工具
查看>>
PHP7新特性 What will be in PHP 7/PHPNG
查看>>
比较strtr, str_replace和preg_replace三个函数的效率
查看>>
ubuntu 下编译PHP5.5.7问题:configure: error: freetype.h not found.
查看>>
PHP编译configure时常见错误 debian centos
查看>>
configure: error: Please reinstall the BZip2 distribution
查看>>
OpenCV gpu模块样例注释:video_reader.cpp
查看>>
【增强学习在无人驾驶中的应用】
查看>>
《python+opencv实践》四、图像特征提取与描述——29理解图像特征
查看>>
《python+opencv实践》四、图像特征提取与描述——30Harris 角点检测
查看>>
《python+opencv实践》四、图像特征提取与描述——31 Shi-Tomasi 角点检测& 适合于跟踪的图像特征
查看>>
OpenCV meanshift目标跟踪总结
查看>>
人工神经网络——神经元模型介绍
查看>>
人工神经网络——感知器介绍
查看>>
人工神经网络——反向传播算法(BackPropagation)
查看>>
进程的地址空间概述
查看>>
Windows 窗口底层原理
查看>>
一种函数指针的运用
查看>>
Win32程序之进程的原理
查看>>
C++虚函数原理
查看>>