Java 如何在接口中查找静态字符串

Java 如何在接口中查找静态字符串,java,string,interface,static,Java,String,Interface,Static,我有以下界面 public static interface Attributes { public final static String InterestDeterminationDate = "InterestDeterminationDate"; public final static String CreditType = "CreditType"; public final static String NumberInternal =

我有以下界面

public static interface Attributes
{
        public final static String InterestDeterminationDate = "InterestDeterminationDate";
        public final static String CreditType = "CreditType";
        public final static String NumberInternal = "NumberInternal";
        public final static String InterestRate = "InterestRate";
        public final static String RemainingDebtAmount = "RemainingDebtAmount";
        public final static String ConsumerPart = "ConsumerPart";
        public final static String TechnicalProductName = "TechnicalProductName";
        public final static String TermOfDuration = "TermOfDuration";
        public final static String PeriodInterestTaxReduction = "PeriodInterestTaxReduction";
        public final static String OriginMark = "OriginMark";
        public final static String Currency = "Currency";
        public final static String PenaltyRuleId = "PenaltyRuleId";
        public final static String InstallmentCalculationMethod = "InstallmentCalculationMethod";
        public final static String InterestRenewalDate = "InterestRenewalDate";
        public final static String TechnicalProductDescription = "TechnicalProductDescription";
        public final static String TechnicalProductDate = "TechnicalProductDate";
        public final static String CollectionIntervalPeriod = "CollectionIntervalPeriod";
        public final static String Enddate = "Enddate";
}
我需要检查一个给定的字符串是否是这个属性接口的一部分

我怎么检查这个

问候,,
bas Hendriks

您可以使用反射API和的“getFields()”方法

然后使用的“getName()”方法检查字段名


以下是。

您可以使用反射API和的“getFields()”方法

然后使用的“getName()”方法检查字段名


下面是.

如果您真的想这样做,那么您应该使用反射并遍历属性中的所有值

更好的方法是使用枚举:

public enum Attributes{
    InterestDeterminationDate,
    CreditType,
    NumberInternal,
    InterestRate,
    RemainingDebtAmount,
    ConsumerPart,
    TechnicalProductName,
    TermOfDuration,
    PeriodInterestTaxReduction,
    OriginMark,
    Currency,
    PenaltyRuleId,
    InstallmentCalculationMethod,
    InterestRenewalDate,
    TechnicalProductDescription,
    TechnicalProductDate,
    CollectionIntervalPeriod,
    Enddate;
}
Attributes.valueOf(变量)将为您检查此项


使用enum时要小心,
valueOf()
方法将在
Attributes
中没有
变量时抛出
IllegalArgumentException
。另外,您的变量
不是
null
,或者您必须处理
NullPointerException

如果您确实想这样做,那么您应该使用反射并遍历属性中的所有值

更好的方法是使用枚举:

public enum Attributes{
    InterestDeterminationDate,
    CreditType,
    NumberInternal,
    InterestRate,
    RemainingDebtAmount,
    ConsumerPart,
    TechnicalProductName,
    TermOfDuration,
    PeriodInterestTaxReduction,
    OriginMark,
    Currency,
    PenaltyRuleId,
    InstallmentCalculationMethod,
    InterestRenewalDate,
    TechnicalProductDescription,
    TechnicalProductDate,
    CollectionIntervalPeriod,
    Enddate;
}
Attributes.valueOf(变量)将为您检查此项


使用enum时要小心,
valueOf()
方法将在
Attributes
中没有
变量时抛出
IllegalArgumentException
。另外,您的变量
不是
null
,或者您必须处理
NullPointerException

您可以使用反射构建一个集合,并对该集合进行测试:

Class<Attributes> attr = Attributes.class;
Field[] fields = attr.getDeclaredFields();
final Set<String> fieldsInAttributes = new HashSet<String>();
for (Field field : fields) {
    fieldsInAttributes.add(field.getName());
}

System.out.println(fieldsInAttributes.contains("PenaltyRuleId"));
Class attr=Attributes.Class;
Field[]fields=attr.getDeclaredFields();
final Set fieldsInAttributes=new HashSet();
用于(字段:字段){
fieldsInAttributes.add(field.getName());
}
System.out.println(fieldsInAttributes.contains(“PenaltyRuleId”);

您可以使用反射构建一个集合,并对该集合进行测试:

Class<Attributes> attr = Attributes.class;
Field[] fields = attr.getDeclaredFields();
final Set<String> fieldsInAttributes = new HashSet<String>();
for (Field field : fields) {
    fieldsInAttributes.add(field.getName());
}

System.out.println(fieldsInAttributes.contains("PenaltyRuleId"));
Class attr=Attributes.Class;
Field[]fields=attr.getDeclaredFields();
final Set fieldsInAttributes=new HashSet();
用于(字段:字段){
fieldsInAttributes.add(field.getName());
}
System.out.println(fieldsInAttributes.contains(“PenaltyRuleId”);

您的问题无法明确您是否试图找出查询字符串是属性名还是属性值。如果您试图找出它是否是一个值,则以下操作将起作用:

public static boolean hasValue(String value) throws IllegalAccessException {
    for(Field field : Attributes.class.getDeclaredFields()) {
        if(((String)field.get(Attributes.class)).equals(value)) {
            return true;
        }
    }
    return false;
}

但是,我建议您在下面使用
枚举
,这样以后使用起来会更方便。

您的问题并没有明确说明您是否在尝试确定查询字符串是属性名还是属性值。如果您试图找出它是否是一个值,则以下操作将起作用:

public static boolean hasValue(String value) throws IllegalAccessException {
    for(Field field : Attributes.class.getDeclaredFields()) {
        if(((String)field.get(Attributes.class)).equals(value)) {
            return true;
        }
    }
    return false;
}
public static String getFieldName(String fieldValue) throws Exception {
  for (Field field : Attributes.class.getFields()) 
    if (fieldValue.equals(field.get(null)))
      return field.getName();
  return null;
}

但是,我建议您在下面使用
枚举
,这样以后使用起来会更方便。

使用“字符串是此属性接口的一部分”是什么意思?使用“字符串是此属性接口的一部分”是什么意思?这将在字段名称中搜索目标字符串。我想他想要的是字段值,而不是名称。它们在示例中是相同的,但是,是的,你可能是对的。这将在字段名称中搜索目标字符串。我想他想要的是字段值,而不是名称。它们在示例中是一样的,但是,是的,你可能是对的。@BasHendriks:另外,如果你想坚持使用字符串(和反射),最好将它们放在最后一个类中,而不是一个接口中。接口并非用于此目的。我同意枚举应该是正确的方法,但我有两个限制:我不能更改接口。我必须用这个。我仅限于Java1.4……然后看看其他解决方案。但是,当你问问题时,你应该指定你的限制。@BasHendriks:另外,如果你想坚持使用字符串(和反射),最好将它们放在最后一个类中,而不是一个接口中。接口并非用于此目的。我同意枚举应该是正确的方法,但我有两个限制:我不能更改接口。我必须用这个。我仅限于Java1.4……然后看看其他解决方案。但是当你问问题时,你应该明确你的限制。我同意科林的观点。他的建议更好,但我仅限于Java1.4(并且不能更改接口),我同意Colin的观点。他的建议更好,但我仅限于Java1.4(并且无法更改接口)
public static String getFieldName(String fieldValue) throws Exception {
  for (Field field : Attributes.class.getFields()) 
    if (fieldValue.equals(field.get(null)))
      return field.getName();
  return null;
}