Skip to content

Commit 3e59fa6

Browse files
committed
优化“反射(reflection)是什么及其用途?”
1 parent 537acad commit 3e59fa6

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ stackoverflow-Java-top-qa
3333
* [ `finally` 代码块总会被执行么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Does-finally-always-execute-in-Java.md)
3434
* [如何将String转换为enum](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/convert-a-string-to-an-enum-in-java.md)
3535
* [在Java中声明数组](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Declare-array-in-Java.md)
36+
* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md.md)
37+
3638

3739
> 编程技巧
3840
@@ -77,7 +79,6 @@ stackoverflow-Java-top-qa
7779
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
7880
- [Fastest way to determine if an integer's square root is an integer](http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer)
7981
- [How to fix: Unsupported major.minor version 51.0 error?](http://stackoverflow.com/questions/10382929/how-to-fix-unsupported-major-minor-version-51-0-error)
80-
- [What is reflection and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful)
8182
- [How to generate a random alpha-numeric string?](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string)
8283
- [Examples of GoF Design Patterns in Java's core libraries](http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns-in-javas-core-libraries)
8384
- [Comparing Java enum members: == or equals()?](http://stackoverflow.com/questions/1750435/comparing-java-enum-members-or-equals)
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
# What is reflection, and why is it useful?
1+
# 反射(reflection)是什么及其用途?
22

33
##问题描述
44
反射是什么,为什么它是有用的?
55
我特别感兴趣的是java,但我认为任何语言的原理都是相同的。
66

77
##回答
8-
反射是被用来描述代码的,可以检查同一系统(或者自身)的其他代码
8+
反射的概念,主要是指程序可以访问、检测和修改它本身状态或行为的一种能力。在java中,通过反射,能够在"运行态"动态获得任意一个类的所有属性和方法,动态地调用对象的方法
99

10-
举个例子,在java中你有一个不知道具体类型的对象,并且你可能会调用它的dosomething的方法(如果存在的话)java的静态类型系统并没有设计来支持此用法,除非对象符合已知的接口。但是用反射,你的代码能查看对象,并找出一个dosomething的方法(如果有的话),然后你可以调用他。
11-
因此,一个例子如下(想象在问题中的对象foo):
10+
举个例子,假设你有一个不知道具体类的对象,并且你想调用它的"dosomething"方法(如果存在的话)。java的静态类型系统只能调用一个已知类对象对应的已知接口,在未指定对象类型时,无法调用它的方法。但是通过反射,你的代码能检查这个未知类对象,并试图找出这个dosomething方法。如果存在这个方法,你可以通过反射调用这个方法。
11+
12+
为了进一步说明,请看下面的例子(下面的对象foo,就是上文提到的,我们不知道它对应的类是什么):
1213
```
1314
Method method = foo.getClass().getMethod("dosomething",null);
1415
method.invoke(foo,null); //调用foo的dosomething方法
1516
```
16-
java中一个非常常见的用例是使用注释。JUnit 4,举个例子,将使用反射来浏览你的标记有@test注释的类方法,之后当运行测试单元时进行调用
17+
反射这个特性,经常会用于各种注解中(annotations)。举个例子,Junit4将使用反射来遍历你的代码,查找所有加了@test注解的类方法,之后运行测试单元时就调用这些方法
1718

1819
[有很多好的反射例子,可以用来入门](http://docs.oracle.com/javase/tutorial/reflect/index.html)
1920

20-
最后,其概念在其他支持反射的静态类型语言中是非常相似的。在动态语言中,上面描述的用例并不是必要的(因为编译器允许任何方法都能被任何对象调用,在运行时如果不存在就会失败),但是第二种情况,寻找能以某种确定的方式工作的方法任然是常见的。
21+
最后,其概念在其他支持反射的静态类型语言中也是非常相似的。在动态语言中,无需用到上面说的第一种用法场景——调用未知类的方法(因为动态语言编允许任意对象调用任意方法,如果不存在对应方法,在运行时就会失败),但是第二种情况,查找做了指定标记的方法,这种场景还是很常见的
2122

22-
[原文链接:What is reflection, and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful)
23+
[stackoverflow链接:What is reflection, and why is it useful?](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful)
2324

2425

0 commit comments

Comments
 (0)