Reflection
Reflection
反射能在运行时获取一个类的全部信息,并且可以调用类方法,修改类属性,创建类实例。
而在编译期间不用关心对象是谁
反射可用在动态代理,注解解释,和反射工厂等地方。
---------------------
public class BasicTest {
public static void main(String[] args) throws Exception {
Demo demo = new Demo(10,"moss");
// 所有类的对象都是Class的实例
Class<?> clazz = null;
Class<?> clazz2 = null;
//获取类对象
clazz = Class.forName("demos.reflection.Demo");
clazz2 = Demo.class;
clazz2 = demo.getClass();
Q.p(clazz.getClass());
Q.p(clazz==clazz2);
// 使用默认构造函数 创建一个新的实例
demo = (Demo) clazz.newInstance();
demo.me();
//获取所有public的构造函数
Constructor<?>[] con =clazz2.getConstructors();
Q.pl(con);
//使用自定义构造函数 创建一个新的实例
demo=(Demo) con[1].newInstance(100,"jack");
demo.me();
//获取超类 接口
Q.p(clazz.getSuperclass());
Q.pl(clazz.getInterfaces());
//获取所有属性,不包括继承的
Field[] fields = clazz.getDeclaredFields();
Q.pl(fields);
//获取无参函数,调用无参函数
Method method=clazz.getMethod("me");
Q.p("method "+method);
method.invoke(clazz.newInstance());
//获取有参函数,调用有参函数
method = clazz.getDeclaredMethod("you", int.class,String.class);
Q.p("method2 "+method);
method.invoke(clazz.newInstance(), 19, "you");
//获取所有方法,不包括父类的, 可通过getMethods()获取全部的
Method[] methods = clazz.getDeclaredMethods();
Q.pl(methods);
//给属性赋值
Field field = clazz.getDeclaredField("name");
field.setAccessible(true);
field.set(demo, "reSetName");
Q.p(demo.getName());
//获取注解
method = clazz.getMethod("toString");
Annotation[] as = method.getDeclaredAnnotations();
Q.pl(as);
}
}
class Father{
protected String father;
}
interface Interface{}
public class Demo extends Father implements Interface{
private static String k = "k";
private Integer id;
private String name;
Demo(){}
public Demo(String name){
this.name=name;
}
public Demo(int id, String name){
this.id=id;
this.name=name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void me() {
Q.p("["+id+"----"+name+"]");
}
public void you(int id, String name){
Q.p("["+id+"----"+name+"]");
}
@Override
public String toString(){
return "["+id+"----"+name+"]";
}
}
---------------------
end
Reflection的更多相关文章
- Fresnel Reflection - 菲涅尔反射
[Fresnel Reflection - 菲涅尔反射] “菲涅尔”是一个人的名字,因为他发现了一个有关反射的光学现象,这个现象就用这个人的名字命名了.那么,是什么现象呢? 这就是反射/折射与视点角度 ...
- CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection)
CSharpGL(13)用GLSL实现点光源(point light)和平行光源(directional light)的漫反射(diffuse reflection) 2016-08-13 由于CSh ...
- Scala Reflection - Mirrors,ClassTag,TypeTag and WeakTypeTag
反射reflection是程序对自身的检查.验证甚至代码修改功能.反射可以通过它的Reify功能来实时自动构建生成静态的Scala实例如:类(class).方法(method).表达式(express ...
- [.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦
[.net 面向对象程序设计进阶] (21) 反射(Reflection)(下)设计模式中利用反射解耦 本节导读:上篇文章简单介绍了.NET面向对象中一个重要的技术反射的基本应用,它可以让我们动态的调 ...
- [.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程
[.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程 本节导读:本节主要介绍什么是.NET反射特性,.NET反射能为我们做些什么,最后介绍几种常用的 ...
- Could not load type 'System.Reflection.AssemblySignatureKeyAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c
错误: Could not load type 'System.Reflection.AssemblySignatureKeyAttribute' from assembly 'mscorlib, V ...
- 代替Reflection(反射)的一些方法
Reflection(反射)是深入学习.Net必须掌握的技能之一.最初学Reflection的时候,的确是被惊住了,原来还可以这样.只要给你一个Assembly, 你就能获取到其中所有的类型,根据类型 ...
- Leetcode: Line Reflection
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...
- 反射(Reflection)
反射主要用于在程序运行期间动态解析相关类的类名,命名空间,属性,方法并进行相应操作,以下通过两个简单的例子进行了说明: 示例1:调用程序集内部方法,运行时动态获取相关类的信息,包括类名,命名空间等信息 ...
- 异常:“System.Reflection.Metadata”已拥有为“System.Collections.Immutable”定义的依赖项
参考动态执行T4模板:https://msdn.microsoft.com/zh-cn/library/bb126579.aspx 我项目是.NET Framework 4.5控制台应用程序写的. 执 ...
随机推荐
- steps animation
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用engine关键字指定该表使用哪个engine
建表及插入数据语句:mysql> create table salary(userid int,salary decimal(9,2));Query OK, 0 rows affected (0 ...
- numpy使用
计算矩阵的一些用法: http://www.360doc.com/content/13/1104/17/9934052_326603249.shtml
- Struts 2.x No result defined for action 异常
这是我跑struts2的第一个例子,跑的也够郁闷的,这个问题烦了我几个钟... 2011-5-10 10:10:17 com.opensymphony.xwork2.util.logging.co ...
- A trip through the Graphics Pipeline 2011_05
After the last post about texture samplers, we’re now back in the 3D frontend. We’re done with verte ...
- TP框架知识点
- AFN 2.6 code报错总结
1. 错误打印 code=-1016 filed: text/html 错误原因:AFN默认不能解析请求回来的text/html数据 解决办法: AFN3.0的请看这里 AFHTTPSessionM ...
- winform最小化后隐藏到右下角,单击或双击后恢复 .
01.//先拖一个notifyIcon控件进来 02. 03.//然后在您的notifyIcon控件中添加 MouseDoubleClick事件,代码如下 04. 05. private void n ...
- Linux下暴力破解工具Hydra详解
一.简介 Number one of the biggest security holes are passwords, as every password security study shows. ...
- ExtJs 使用点滴 十四 通过设置CheckboxSelectionModel属性值来实现GridPanel复选框可用不可用
var sm = new Ext.grid.CheckboxSelectionModel({singleSelect : false,renderer:function(v, p, record) ...