使用特定枚举类的超类型参数
我有两个解析器类,我想在解析失败时抛出异常。 我想使用相同的异常 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,您可以让
ParserException
存储一个Enum
,但这确实不会为您提供比仅使用 < code>Object 作为ParserException
中字段的类型。如果不改变您在这里所做的设计,我的偏好是创建一个名为
Field
的标记接口,并让所有表示字段的enum
实现该接口。 .. 那么ParserException
可以使用Field
作为它存储的对象类型。在大多数情况下,枚举应该是一个实现细节,其他任何东西都不需要关心。
Well, you could have
ParserException
store anEnum<?>
, but this really doesn't provide you any real advantage over just usingObject
as the type for the field inParserException
.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 yourenum
s that represent fields implement that... thenParserException
could useField
as the type of object it stores.For the most part, that something is an
enum
should be an implementation detail that nothing else should have to care about.不是 enum,但 Enum 是常见的超类型(或更准确地说
枚举
)。但可能你不想使用它,它太笼统了。使用类似的东西可能会更好但是,大多数时候最好创建单独的 Exception 子类。
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 likeHowever, most of the time it's better to create separate subclasses of Exception.
只需使用
Fields
类型,就像使用普通类一样。其他情况也类似。
顺便说一句,枚举通常以单数命名(同样,就像类一样,枚举没有什么不同)。
编辑
您可以使用
Enum
类(所有枚举的公共超类),甚至Object
。但坦率地说,我认为用一个元素声明枚举类没有什么意义。您可以只使用字符串常量:功能相同且不易混淆。
Just use
Fields
type, like you would do with normal class.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 evenObject
.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.
ParserA.Fields
和ParserB.Fields
没有共同的超类(原始类型Enum
或Object
除外) ,但我怀疑这对你有帮助)。由于枚举无法扩展,我不知道如何使用枚举来解决这个问题。
您可以通过将
enum
更改为class
并让ParserA.Field
和ParserB.Field
扩展来解决此问题代码>ParserException.Field。然而,为了具有与枚举相同的优点,您必须限制实例的数量。有多种方法可以实现此目的,例如使构造函数受保护,并具有有限数量的该类型的公共静态成员。ParserA.Fields
andParserB.Fields
have no common super class, (except for the raw typeEnum
orObject
, 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
intoclass
and let bothParserA.Field
andParserB.Field
extendParserException.Field
. In order to have the same advantages asenums
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.