如何循环在Java类的属性? [英] How to loop over a Class attributes in Java?
问题描述
我怎么能遍历一类属性的动态java中。
How can I loop over a class attributes in java dynamically.
有关例如:
public class MyClass
{
private type1 att1;
private type2 att2;
...
public void function()
{
for(var in MyClass.Attributes)
{
System.out.println(var.class);
}
}
}
在Java是这可能吗?
is this possible in Java?
推荐答案
有没有语言支持,你要问什么。
There is no linguistic support to do what you're asking for.
您可以使用反射反射地访问类型的成员在运行时(例如用<一个href=\"http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getDeclaredFields%28%29\"><$c$c>Class.getDeclaredFields()$c$c>获得 字段的数组
),但是这取决于你想要做什么,这可能不是最好的解决方案。
You can reflectively access the members of a type at run-time using reflection (e.g. with Class.getDeclaredFields()
to get an array of Field
), but depending on what you're trying to do, this may not be the best solution.
- Java Tutorials: Reflection API / Advanced Language Topics: Reflection
- What is reflection, and why is it useful?
- Java Reflection: Why is it so bad?
- How could Reflection not lead to code smells?
- Dumping a java object’s properties
下面是一个简单的例子只是为了说明一些什么反映是能够做到的事情。
Here's a simple example to show only some of what reflection is capable of doing.
import java.lang.reflect.*;
public class DumpFields {
public static void main(String[] args) {
inspect(String.class);
}
static <T> void inspect(Class<T> klazz) {
Field[] fields = klazz.getDeclaredFields();
System.out.printf("%d fields:%n", fields.length);
for (Field field : fields) {
System.out.printf("%s %s %s%n",
Modifier.toString(field.getModifiers()),
field.getType().getSimpleName(),
field.getName()
);
}
}
}
上面的代码中使用反射来检查的 String类
所有声明的字段;它产生的输出如下:
The above snippet uses reflection to inspect all the declared fields of class String
; it produces the following output:
7 fields:
private final char[] value
private final int offset
private final int count
private int hash
private static final long serialVersionUID
private static final ObjectStreamField[] serialPersistentFields
public static final Comparator CASE_INSENSITIVE_ORDER
有效的Java第二版,第53条:preFER接口反射
这是从书中摘录:
Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection
These are excerpts from the book:
> 对象,你可以获取<一个href=\"http://java.sun.com/javase/6/docs/api/java/lang/reflect/Constructor.html\"><$c$c>Constructor$c$c>, 方法
一>,和 字段
情况下重新presenting类的构造函数,方法和字段。 [他们]让你操纵他们的潜在对手沉思的。这股力量,但是,是有代价的:
Given a
Class
object, you can obtainConstructor
,Method
, andField
instances representing the constructors, methods and fields of the class. [They] let you manipulate their underlying counterparts reflectively. This power, however, comes at a price:
- 您失去编译时检查的所有好处。
- 要执行反射访问所需的code是笨拙和详细。
- 性能会受到影响。
作为一项规则,对象不应该沉思在正常的应用程序在运行时访问。
As a rule, objects should not be accessed reflectively in normal applications at runtime.
有需要反思的一些复杂的应用程序。例子包括的 [...省略了目的...] 的如果您有任何怀疑你的应用是否属于这些类别之一,它可能没有。
There are a few sophisticated applications that require reflection. Examples include [...omitted on purpose...] If you have any doubts as to whether your application falls into one of these categories, it probably doesn't.
这篇关于如何循环在Java类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!