子类检查,是运算符还是枚举检查
几个朋友正在讨论继承的使用以及如何检查子类是否属于特定类型,我们决定将其发布在 Stack 上。争论的焦点是是否应该在基类中实现一个抽象枚举以用于检查子类的类型,或者是否应该使用 is 运算符。
Alt 1.
public abstract class Document{
}
public class PDF:Document{
}
Check: If (myobj is PDF)
Alt 2.
public abstract class Document{
public abstract DucumentType TypeOfDocument {get;}
}
public class PDF:Document{
public DucumentType TypeOfDocument { get{return DucumentType.PDF };}
}
public enum DucumentType{
PDF, Word
}
Check: If (myobj.TypeOfDocument == DucumentType.PDF)
Alt1 的那些。如果 Alt2 稍微破坏了 SRP,那么您就没有利用 OO,您重复了抽象。由于继承是类之间最难的连接,因此您无法避免了解它们,如果必须进行继承,请将影响降至最低。 Alt2 也打破了 DRY
Alt2 的规定,Alt2 将完全删除类型检查,并用检查此枚举的选项替换它。删除与所有子类的所有硬连接,并且枚举本身的值并没有说明当前正在操作哪个具体实现。
您对这两种选择有何看法?
没有讨论继承与组合等,这是另一个问题!
A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a abstract enum in the base class to be used for checking the type of the subclass, or if you should use the is operator.
Alt 1.
public abstract class Document{
}
public class PDF:Document{
}
Check: If (myobj is PDF)
Alt 2.
public abstract class Document{
public abstract DucumentType TypeOfDocument {get;}
}
public class PDF:Document{
public DucumentType TypeOfDocument { get{return DucumentType.PDF };}
}
public enum DucumentType{
PDF, Word
}
Check: If (myobj.TypeOfDocument == DucumentType.PDF)
The ones for Alt1. ment that Alt2 slightly breaks SRP, you don’t take advantage of OO, Your repeating the abstraction. Since inheritance is the hardest connection between classes you cannot avoid knowing of them, and if you must go thru with inheritance minimize the impact. Alt2 also breaks DRY
The ones for Alt2 ment that Alt2 will be removing type checking entirely and replacing it with the option of checking this enum instead. Removing all hard connections to all subclasses, and the value of the enum itself does not say anything about which concrete implementation thats currently beeing operated on.
Whats your opinion about the two alternatives?
No discussion of inheritance vs. composition etcetera, that’s another question!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么你首先需要知道?我同意它偶尔是必要的,但在可能的情况下,您应该使文档类型具有适当的抽象功能,以允许通过继承完成专业化,而不是调用者必须以不同的方式对待它。
如果不同的子类可以共享文档类型但又不想共享继承层次结构,我只会使用枚举方法。 IME,这种情况非常罕见。
Why do you need to know in the first place? I agree that it's occasionally necessary, but where possible you should make the Document type have appropriate abstract functionality to allow the specialization to be done through inheritance rather than the caller having to treat it differently.
I would only use the enum approach if different subclasses may share document types but withotu wanting to share an inheritance hierarchy. This would be pretty rare, IME.
IMO 你应该使用 is 运算符。
它为您提供相同的结果,而不会污染(抽象)类代码。
IMO you should use the is operator.
It gives you the same result without tainting the (abstract) class code.
我也遇到过类似的情况,只是在我的情况下,DocumentType 枚举需要随着各种类型的添加而增长。通过使用 Enum,类型检查要好得多,但它要求每次添加新的 DocumentType 时都重新编译“通用”基类。
我目前正在考虑的替代方案是使用接口属性将类型作为 STRING 返回。它对于类型检查来说不是很好,但我的代码的其余部分具有必要的验证以防止恶意 DocumentType 对象。我更喜欢不同的解决方案,但什么也没想到。
I've got a similar situation, except that in my case, the DocumentType enum needs to grow as various types are added. By using the Enum, type checking is much better, but it requires that the "generic" base class be recompiled every time a new DocumentType is added.
The alternative I'm currently pondering is to use an interface property to return the type as a STRING. It's not great for type checking, but the rest of my code has the necessary validation to prevent rogue DocumentType objects. I would prefer a different solution, but nothing comes to mind.