受保护的构造函数和 MustInherit/ 抽象类

发布于 2024-07-26 18:33:38 字数 670 浏览 5 评论 0原文

具有受保护构造函数的类与标记为 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 技术交流群。

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

发布评论

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

评论(3

久伴你 2024-08-02 18:33:38

您可以使用反射调用 Protected 构造函数并实例化该类,但不能以这种方式实例化 abstract 类。 您可以在 MustInherit 类中声明 MustOverride 方法,但 Protected 构造函数不能对派生类强制执行任何操作。

您应该始终将概念上抽象的类声明为 MustInherit。 当您将受保护的构造函数与一些公共重载一起提供以向派生类提供更多功能时,受保护的构造函数会很有用。

You can call the Protected constructor using reflection and instantiate the class but you can't instantiate an abstract class in this way. You can declare MustOverride methods in MustInherit classes but Protected 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 some Public overloads to provide some more functionality to derived classes.

半﹌身腐败 2024-08-02 18:33:38

如果类只有一个受保护的构造函数,仍然可以有一个独立的类实例。 这需要解决受保护的构造函数,例如使用反射。

如果该类被标记为 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.

看春风乍起 2024-08-02 18:33:38

不太确定你想要什么。

如果您需要创建抽象类的对象,我建议您创建抽象类的私有类实现,并在 CreateInstanceMethod 中返回它:

Public MustInherit MyBaseClass
    Private BaseClassImplementation
        Inherits MyBaseClass

        ...
    End Class

    Public Function CreateInstance(paramList) as MyBaseClass
        If paramList Is Ok Then Return New BaseClassImplementation
    End Function
End Class

但是,如果您想向构造添加一些约束,我建议抛出异常:

Public MustInherit MyBaseClass
    Protected Sub New(paramList)
        If paramList IsNot Ok Then Thow New Exception
        ...
    End Sub
End Class

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:

Public MustInherit MyBaseClass
    Private BaseClassImplementation
        Inherits MyBaseClass

        ...
    End Class

    Public Function CreateInstance(paramList) as MyBaseClass
        If paramList Is Ok Then Return New BaseClassImplementation
    End Function
End Class

However, if you want to add some constraints to the construction, I recommend to throw exceptions:

Public MustInherit MyBaseClass
    Protected Sub New(paramList)
        If paramList IsNot Ok Then Thow New Exception
        ...
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文