1. <i id='qrf59'><tr id='qrf59'><dt id='qrf59'><q id='qrf59'><span id='qrf59'><b id='qrf59'><form id='qrf59'><ins id='qrf59'></ins><ul id='qrf59'></ul><sub id='qrf59'></sub></form><legend id='qrf59'></legend><bdo id='qrf59'><pre id='qrf59'><center id='qrf59'></center></pre></bdo></b><th id='qrf59'></th></span></q></dt></tr></i><div id='qrf59'><tfoot id='qrf59'></tfoot><dl id='qrf59'><fieldset id='qrf59'></fieldset></dl></div>

      <tfoot id='qrf59'></tfoot>
        <bdo id='qrf59'></bdo><ul id='qrf59'></ul>
      1. <small id='qrf59'></small><noframes id='qrf59'>

        <legend id='qrf59'><style id='qrf59'><dir id='qrf59'><q id='qrf59'></q></dir></style></legend>
      2. 如何在Java中循环一个类属性?

        时间:2023-09-27
        <i id='eA1lk'><tr id='eA1lk'><dt id='eA1lk'><q id='eA1lk'><span id='eA1lk'><b id='eA1lk'><form id='eA1lk'><ins id='eA1lk'></ins><ul id='eA1lk'></ul><sub id='eA1lk'></sub></form><legend id='eA1lk'></legend><bdo id='eA1lk'><pre id='eA1lk'><center id='eA1lk'></center></pre></bdo></b><th id='eA1lk'></th></span></q></dt></tr></i><div id='eA1lk'><tfoot id='eA1lk'></tfoot><dl id='eA1lk'><fieldset id='eA1lk'></fieldset></dl></div>

              <tbody id='eA1lk'></tbody>

            <small id='eA1lk'></small><noframes id='eA1lk'>

            <tfoot id='eA1lk'></tfoot>

              1. <legend id='eA1lk'><style id='eA1lk'><dir id='eA1lk'><q id='eA1lk'></q></dir></style></legend>

                  <bdo id='eA1lk'></bdo><ul id='eA1lk'></ul>
                  本文介绍了如何在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.

                  您可以在运行时使用反射(例如,使用 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.

                  • Java 教程:反射 API/高级语言主题:反射
                  • 什么是反射,为什么它有用?
                  • Java 反射:为什么这么糟糕?李>
                  • 反射怎么可能不会导致代码异味?
                  • 转储 java 对象的属性

                  这是一个简单的示例,仅展示反射的部分功能.

                  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,第 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.

                  这篇关于如何在Java中循环一个类属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何从 Lucene TokenStream 中获取 Token? 下一篇:是否可以使用 Java Reflection 打印出父类的属性?

                  相关文章

                  最新文章

                  <i id='oTQqm'><tr id='oTQqm'><dt id='oTQqm'><q id='oTQqm'><span id='oTQqm'><b id='oTQqm'><form id='oTQqm'><ins id='oTQqm'></ins><ul id='oTQqm'></ul><sub id='oTQqm'></sub></form><legend id='oTQqm'></legend><bdo id='oTQqm'><pre id='oTQqm'><center id='oTQqm'></center></pre></bdo></b><th id='oTQqm'></th></span></q></dt></tr></i><div id='oTQqm'><tfoot id='oTQqm'></tfoot><dl id='oTQqm'><fieldset id='oTQqm'></fieldset></dl></div>
                  <tfoot id='oTQqm'></tfoot>

                  <small id='oTQqm'></small><noframes id='oTQqm'>

                        <bdo id='oTQqm'></bdo><ul id='oTQqm'></ul>
                    1. <legend id='oTQqm'><style id='oTQqm'><dir id='oTQqm'><q id='oTQqm'></q></dir></style></legend>