使用特定枚举类的超类型参数

发布于 2024-10-14 19:43:04 字数 905 浏览 0 评论 0原文

我有两个解析器类,我想在解析失败时抛出异常。 我想使用相同的异常 ParserException,我希望它可以接受导致失败的字段名称。我想使用枚举,但我认为我没有完全清楚这个主题。

如何在 ParserException 类中声明 fieldName? 据我了解,枚举应该是 ParserA.Fields 和 ParserB.Fields 的超类型,但不被接受。

请注意,两个枚举类包含一组不同的枚举,即它们不是同一个类。

public class ParserA {

 public enum Fields {
  A_FIRST_FIELD
  A_SECOND_FIELD
 }


 public void parse() {
  ... 
 throw ParserException(Fields.A_FIRST_FIELD);  
 }

}

public class ParserB {

 public enum Fields {
  B_FIRST_FIELD
  B_SECOND_FIELD
 }

 public void parse() {
  ... 
  throw ParserException(Fields.B_FIRST_FIELD);  
 }

}



// Parser error
public class ParserException extends Exception {

 enum fieldName; // ????? what goes here?


 public ParserException(enum e) {
  this.fieldName = e;
 }

 public enum getFieldName() {  // ?????? how do I do something like this?
  return fieldName;
 }

}

I have two parser classes, and I want to throw an exception when parsing fails.
I want to use the same exception, ParserException, which I'd like could accept the field name which caused the failure. I thought to use enums, but I think I don't have the topic completely clear.

How do I declare fieldName in the ParserException class? enum, as far as I understand, should be the supertype for ParserA.Fields and ParserB.Fields, but is not accepted.

Please note that the two enum classes contain a different set of enums, i.e. they are not the same class.

public class ParserA {

 public enum Fields {
  A_FIRST_FIELD
  A_SECOND_FIELD
 }


 public void parse() {
  ... 
 throw ParserException(Fields.A_FIRST_FIELD);  
 }

}

public class ParserB {

 public enum Fields {
  B_FIRST_FIELD
  B_SECOND_FIELD
 }

 public void parse() {
  ... 
  throw ParserException(Fields.B_FIRST_FIELD);  
 }

}



// Parser error
public class ParserException extends Exception {

 enum fieldName; // ????? what goes here?


 public ParserException(enum e) {
  this.fieldName = e;
 }

 public enum getFieldName() {  // ?????? how do I do something like this?
  return fieldName;
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情何以堪。 2024-10-21 19:43:04

好吧,您可以ParserException 存储一个 Enum,但这确实不会为您提供比仅使用 < code>Object 作为 ParserException 中字段的类型。

如果不改变您在这里所做的设计,我的偏好是创建一个名为 Field 的标记接口,并让所有表示字段的 enum 实现该接口。 .. 那么 ParserException 可以使用 Field 作为它存储的对象类型。

public interface Field {
}

...

public enum Fields implements Field {
  A_FIRST_FIELD,
  A_SECOND_FIELD
}

...

public class ParserException extends Exception {
  private final Field field;

  public ParserException(Field field) {
    this.field = field;
  }

  public Field getField() {
    return field;
  }
}

在大多数情况下,枚举应该是一个实现细节,其他任何东西都不需要关心。

Well, you could have ParserException store an Enum<?>, but this really doesn't provide you any real advantage over just using Object as the type for the field in ParserException.

Short of changing the design of what you're doing here, my preference would be to make a marker interface called Field and have all your enums that represent fields implement that... then ParserException could use Field as the type of object it stores.

public interface Field {
}

...

public enum Fields implements Field {
  A_FIRST_FIELD,
  A_SECOND_FIELD
}

...

public class ParserException extends Exception {
  private final Field field;

  public ParserException(Field field) {
    this.field = field;
  }

  public Field getField() {
    return field;
  }
}

For the most part, that something is an enum should be an implementation detail that nothing else should have to care about.

记忆で 2024-10-21 19:43:04

据我了解,枚举应该是 ParserA.Fields 和 ParserB.Fields 的超类型

不是 enum,但 Enum 是常见的超类型(或更准确地说 枚举)。但可能你不想使用它,它太笼统了。使用类似的东西可能会更好

 // ParserExceptionKind is probably a better name
 public interface ParserEnum { 
      String name();
      // some useful methods go here
 }

 public class ParserA implements ParserEnum {
      public enum Fields {
           A_FIRST_FIELD
      }
 }

 public class ParserException extends Exception {
      private ParserEnum parserEnum;
      ...
 }

但是,大多数时候最好创建单独的 Exception 子类。

enum, as far as I understand, should be the supertype for ParserA.Fields and ParserB.Fields

Not enum, but Enum is the common supertype (or more precisely Enum<?>). But probably you don't want to use it, it's too general. It could be better to use something like

 // ParserExceptionKind is probably a better name
 public interface ParserEnum { 
      String name();
      // some useful methods go here
 }

 public class ParserA implements ParserEnum {
      public enum Fields {
           A_FIRST_FIELD
      }
 }

 public class ParserException extends Exception {
      private ParserEnum parserEnum;
      ...
 }

However, most of the time it's better to create separate subclasses of Exception.

玩世 2024-10-21 19:43:04

只需使用 Fields 类型,就像使用普通类一样。

Fields fieldName;

其他情况也类似。

顺便说一句,枚举通常以单数命名(同样,就像类一样,枚举没有什么不同)。

编辑
您可以使用Enum类(所有枚举的公共超类),甚至Object

但坦率地说,我认为用一个元素声明枚举类没有什么意义。您可以只使用字符串常量:功能相同且不易混淆。

Just use Fields type, like you would do with normal class.

Fields fieldName;

And similar in other cases.

BTW, enums are usually named in singular case (again, just like with classes, enums aren't that different).

edit
You can use Enum class (common superclass for all enums), or even Object.

But frankly, I see little point in declaring enum class with one element. You could've just used string constants: same functionality and less confusing.

莫相离 2024-10-21 19:43:04

ParserA.FieldsParserB.Fields 没有共同的超类(原始类型 EnumObject 除外) ,但我怀疑这对你有帮助)。

由于枚举无法扩展,我不知道如何使用枚举来解决这个问题。

您可以通过将 enum 更改为 class 并让 ParserA.FieldParserB.Field 扩展来解决此问题代码>ParserException.Field。然而,为了具有与枚举相同的优点,您必须限制实例的数量。有多种方法可以实现此目的,例如使构造函数受保护,并具有有限数量的该类型的公共静态成员。

ParserA.Fields and ParserB.Fields have no common super class, (except for the raw type Enum or Object, but I doubt that that's going to help you here).

Since enums can't be extended, I don't see how you would be able to solve this using enums.

You could work around this by changing enum into class and let both ParserA.Field and ParserB.Field extend ParserException.Field. In order to have the same advantages as enums however, you would have to limit the number of instances. There are several ways of achieving this, such as making the constructor protected, and have a limited number of public static members of this type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文