如何通过其价值获得房产的名称?

时间:2015-12-17 17:06:22

标签: java reflection field

我有一个班级:

public class Something{
    public static final int SOMETHING_0 = 0;
    public static final int SOMETHING_1 = 1;
    public static final int SOMETHING_2 = 2;
    // ... etc
}

值是唯一的,如何通过其值获取属性的名称?

通过反思,例如:

// intValue = 1
String getNameByValue(int intValue){
    ...
    // returns "SOMETHING_1"
    return nameOfProperty;
} 

2 个答案:

答案 0 :(得分:3)

你可以用这种方式做到这一点:

 public class Something {
        public static final int SOMETHING_0 = 0;
        public static final int SOMETHING_1 = 1;
        public static final int SOMETHING_2 = 2;

    }

 String getNameByValue(int intValue) throws IllegalArgumentException,
        IllegalAccessException {
    for (Field field : Something.class.getFields()) {
        if (field.getInt(null) == intValue) {
            return field.getName();
        }
    }
    return "";
}

如果你这样做,圣诞老人将邮寄并屠杀十几只无辜的小猫。

我们可以以正确的方式执行此操作,将您的值实现为枚举

public enum Something {
        SOMETHING_0(0),
        SOMETHING_1(1),
        SOMETHING_2(2);
        final int val;
        Something(int val){
            this.val = val;
        }
        static public  Something byNumber(int number){
            for (Something somthing : Something.values()){
                if (number==somthing.val){
                    return somthing;
                }
            }
            throw new RuntimeException("Value not found");
        }

    }

答案 1 :(得分:1)

一种选择是:

// if $propName = 'mike';
$this->{$propName} = 'X';
// Results in:
$this->mike = 'X';

// if $propName = 'cart';
$this->{$propName} = 'full';
// Results in:
$this->cart = 'full';

// if $propName = 'version';
$this->{$propName} = 3;
// Results in:
$this->version = 3;
public class Something {
    public static final int SOMETHING_0 = 0;
    public static final int SOMETHING_1 = 1;
    public static final int SOMETHING_2 = 2;
    public String toString(int value) {
        switch (value) {
            case SOMETHING_0: return "SOMETHING_0";
            case SOMETHING_1: return "SOMETHING_1";
            case SOMETHING_2: return "SOMETHING_2";
        }
        throw new IllegalArgumentException("Unknown value: " + value);
    }
}

如果// Use: int value = Something.SOMETHING_1; String name = Something.toString(value); 值是连续的,从int开始,那么您可以使用0及其enum位置:

ordinal()
public enum Something {
    SOMETHING_0, SOMETHING_1, SOMETHING_2
}

如果// Use: int value = Something.SOMETHING_1.ordinal(); String name = Something.values()[value].name(); // or toString() 连续,您仍然可以使用int

enum
enum Something {
    SOMETHING_0(0),
    SOMETHING_10(10),
    SOMETHING_20(20);
    private final int value;
    private Something(int value) {
        this.value = value;
    }
    public int getValue() {
        return this.value;
    }
    public static Something from(int value) {
        for (Something something : values())
            if (something.value == value)
                return something;
        throw new IllegalArgumentException("Unknown value: " + value);
    }
}

或者,如果您希望显示文本与常量名称不同,并且您不喜欢异常,则可以执行以下操作:

// Use:
int value = Something.SOMETHING_10.getValue();
String name = Something.from(value).name(); // or toString()
enum Something {
    SOMETHING_0 ( 0, "Something 0"),
    SOMETHING_10(10, "Something 10"),
    SOMETHING_20(20, "Something 20");
    private final int    value;
    private final String description;
    private Something(int value, String description) {
        this.value = value;
        this.description = description;
    }
    public int getValue() {
        return this.value;
    }
    public String getDescription() {
        return this.description;
    }
    public static Optional<Something> from(int value) {
        for (Something something : values())
            if (something.value == value)
                return Optional.of(something);
        return Optional.empty();
    }
    public static String toString(int value) {
        for (Something something : values())
            if (something.value == value)
                return something.description;
        return "Unknown value (" + value + ")";
    }
}