8

簡単な質問があります。クラスのすべての静的値を反復する方法はありますか (リフレクションを使用すると思います)。

例えば

class Any {
    static int one = 1;
    static int two = 2;
    static int three = 3;

    public static void main( String [] args ) {
          for( int i : magicMethod( Any.class ) ){
              System.out.println( i );
          }
    }
 }

出力

 1
 2
 3

ありがとう。

4

5 に答える 5

18
import java.util.*;
import java.lang.reflect.*;

class Any {
    static int one = 1;
    static int two = 2;
    static int three = 3;

    public static void main( String [] args ) {
          for( int i : magicMethod( Any.class ) ){
              System.out.println( i );
          }
    }

    public static Integer[] magicMethod(Class<Any> c) {
        List<Integer> list  = new ArrayList<Integer>();
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            try {
                if (field.getType().equals(int.class) && Modifier.isStatic(field.getModifiers())) {
                    list.add(field.getInt(null));
                }
            }
            catch (IllegalAccessException e) {
                // Handle exception here
            }
        }
        return list.toArray(new Integer[list.size()]);
    }
 }
于 2008-11-07T03:24:23.537 に答える
4

ねえ.. とても簡単でした。:P

      Field [] constants = Main.class.getFields();
      Object some = new Main();
      for( Field field : constants ){
          if(Modifier.isStatic(field.getModifiers() ) && 
             field.getType() == int.class  ) {
                    System.out.println( field.getInt( some  ) );
          }
      }
于 2008-11-07T03:09:16.487 に答える
2

あなたのソリューションはパブリック フィールドでは機能しますが、例のようなプライベート フィールドでは機能しません。クラスのプライベート フィールドにアクセスできるようにするには、getFields() の代わりに getDeclaredFields() を使用する必要があります。

于 2008-11-07T03:13:14.077 に答える
1

次のようなことができます。

import java.lang.reflect.*;

public class Foo {

public static int one = 1;
public static int two = 2;
public static int three = 3;

public static void magicMethod( Class clz ) throws Exception {
    Field[] fields = clz.getFields();
    System.out.println(""+fields);
    for( Field field : fields ) {
        int modifiers = field.getModifiers();
        if( ! Modifier.isStatic(modifiers) )
            continue;
        System.out.println("" + field.get(null));
    }
}

public static void main(String[] args) throws Exception {
    Foo.magicMethod( Foo.class );
}}

ただし、これが機能するためには、フィールドがパブリックである必要があることに注意することが重要です。それはまさにあなたが求めたものではありませんが、必要なもののために機能させることができるほど十分に近いはずです. 明らかに、これはエラー処理などを行わないため、実際のアプリケーションでエラーや例外を処理するようにする必要があります。

于 2008-11-07T03:13:01.750 に答える
0

別の方法として、列挙型を使用してクラス変数を完全に削除します (正確には、列挙型もクラス変数です:-)):

class Any {
    enum Number {
        ONE(1),
        TWO(2),
        THREE(3);

        Number(int number) {
            this.number = number;
        }

        int number;
    };

    public static void main(String [] args) {
        for (Number value : Number.values()) {
            System.out.println(value.number);
        }
    }
}
于 2011-11-03T09:25:59.733 に答える