简体   繁体   English

Java类文字和反思?

[英]Java Class Literals and Reflection?

I'm pretty new to java, and on the java tutorial it uses the terms "class literal" and "reflection". 我对java很新,在java教程中它使用了术语“class literal”和“reflection”。

From the reflection api trail from the java website, http://docs.oracle.com/javase/tutorial/reflect/index.html , it says 来自java网站http://docs.oracle.com/javase/tutorial/reflect/index.html的反射api路径,它说

Extensibility Features An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names. 可扩展性功能应用程序可以通过使用完全限定名称创建可扩展性对象的实例来使用外部用户定义的类。

What does that mean? 这意味着什么?

Thanks. 谢谢。

A class literal is something like String.class , ie a compile-time literal representing the String class. 类文字类似于String.class ,即表示String类的编译时文字。

In short, reflection is a language feature that allows you to "reflect" on the code, ie you can query the system for classes, methods, fields, etc. and use that data to create new instances, call methods or change the value of fields. 简而言之,反射是一种语言功能,允许您“反映”代码,即您可以查询系统中的类,方法,字段等,并使用该数据创建新实例,调用方法或更改值领域。

Reflection might be useful to create objects of classes that are not known at compile-time, but are on the classpath at runtime. 反射可能对创建在编译时未知但在运行时位于类路径上的类的对象很有用。 Several extension frameworks make use of that, mostly by providing fully qualified class names in some text file (eg com.acme.SomeFancyClass ), getting the associated Class object from the class loader and creating new instances. 几个扩展框架使用它,主要是通过在某些文本文件(例如com.acme.SomeFancyClass )中提供完全限定的类名,从类加载器获取关联的Class对象并创建新实例。

Other frameworks (eg Apache Common's builder objects, OGNL, Java Beans etc.) use reflection to query the available properties (getters and/or matching setters) that can be accessed (through calls to those getters/setters). 其他框架(例如Apache Common的构建器对象,OGNL,Java Bean等)使用反射来查询可以访问的可用属性(getter和/或匹配setter)(通过调用那些getter / setter)。

However, if you are new to Java, I'd recommend diving into other language features before loosing yourself in the depth of the reflection facility. 但是,如果您是Java新手,我建议您先深入了解其他语言功能,然后再深入反射设施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM