• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

循环 Java 的类属性?

用户头像
it1352
帮助1

问题说明

如何在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?

正确答案

#1

没有语言支持来完成您的要求.

There is no linguistic support to do what you're asking for.

您可以在运行时使用反射(例如使用 Class.getDeclaredFields() 获取Field),但取决于你想要做什么,这可能不是最好的解决方案.

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.

这里有一个简单的例子,只展示了反射能够做的一些事情.

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()
            );
        }
    }
}

上面的代码片段使用反射来检查class 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

Effective Java 2nd Edition,Item 53:首选接口而非反射

以下是本书的摘录:


Effective Java 2nd Edition, Item 53: Prefer interfaces to reflection

These are excerpts from the book:

给定一个 Class 对象,可以获取构造函数方法Field 实例表示类的构造函数、方法和字段.[它们] 让你反射性操纵它们的底层对应物.然而,这种力量是有代价的:

Given a Class object, you can obtain Constructor, Method, and Field 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:

  • 您失去了编译时检查的所有好处.
  • 执行反射访问所需的代码笨拙而冗长.
  • 性能受到影响.

通常,在正常应用程序中,不应在运行时反射性地访问对象.

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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfiecc
系列文章
更多 icon
同类精品
更多 icon
继续加载