受保护的构造函数和 MustInherit/ 抽象类
具有受保护构造函数的类与标记为 MustInherit
的类有什么区别? (我正在使用 VB.Net 进行编程,但它可能同样适用于 C#)。
我问的原因是因为我有一个抽象类,我想将其构造函数转换为共享/静态方法。 (添加一些约束)。
我无法执行此操作,因为无法在共享函数中创建实例。
我想删除 MustInherit
关键字。 这会有什么不同吗?
谢谢。
ETA:
我想我已经回答了我的问题,如果我删除 MustInherit 关键字,我就不能再包含 MustOverrides,这非常有用。
考虑到这一点,有什么办法可以解决我的问题吗?
ETA2:
澄清一下,除非删除 MustInherit 关键字,否则我无法执行以下操作?
Public MustInherit MyBaseClass
Private Sub New()
End Sub
Protected Function CreateInstance(ParmList) As MyBaseClass
If ParmList is Ok Then Return New MyBaseClass()
End Function
End Class
What is the difference between a class with protected constructors and a class marked as MustInherit
? (I'm programming in VB.Net but it probably equally applies to c#).
The reason I ask is because I have an abstract class that I want to convert the constructors to shared/static methods. (To add some constraints).
I can't do this because it's not possible to create an instance in the shared function.
I'm thinking to just remove the MustInherit
keyword. Will this make any difference?
Thanks.
ETA:
I think i've answered my question, If I remove the MustInherit keyword, I can no longer include the MustOverrides, which are very useful.
With that in mind, is there any way around my problem?
ETA2:
To clarify, I can't do the below unless I remove the MustInherit keyword?
Public MustInherit MyBaseClass
Private Sub New()
End Sub
Protected Function CreateInstance(ParmList) As MyBaseClass
If ParmList is Ok Then Return New MyBaseClass()
End Function
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用反射调用
Protected
构造函数并实例化该类,但不能以这种方式实例化abstract
类。 您可以在MustInherit
类中声明MustOverride
方法,但Protected
构造函数不能对派生类强制执行任何操作。您应该始终将概念上抽象的类声明为
MustInherit
。 当您将受保护的构造函数与一些公共重载一起提供以向派生类提供更多功能时,受保护的构造函数会很有用。You can call the
Protected
constructor using reflection and instantiate the class but you can't instantiate anabstract
class in this way. You can declareMustOverride
methods inMustInherit
classes butProtected
constructor can enforce nothing on derived classes.You should always declare classes that are conceptually abstract as
MustInherit
.Protected
constructors can be useful when you are providing it along with somePublic
overloads to provide some more functionality to derived classes.如果类只有一个受保护的构造函数,仍然可以有一个独立的类实例。 这需要解决受保护的构造函数,例如使用反射。
如果该类被标记为 MustInherit,则不可能单独拥有该类的实例。 只能创建派生/继承类的实例。
If the class only has a protected constructor, it is still possible to have an instance of the class which can stand on its own. It would require working around the protected constructor, such as using reflection.
If the class is marked as MustInherit, it is impossible to have an instance of that class on its own. Instances can only be created of the derived/inherited classes.
不太确定你想要什么。
如果您需要创建抽象类的对象,我建议您创建抽象类的私有类实现,并在 CreateInstanceMethod 中返回它:
但是,如果您想向构造添加一些约束,我建议抛出异常:
Not really sure what you want.
If you need to create an object of the abstract class, I recommend you create a private class implementation of your abstract class and return it in your CreateInstanceMethod:
However, if you want to add some constraints to the construction, I recommend to throw exceptions: