Overridable Subroutine Without Default Behavior?
In a base class object, I have an overridable subroutine that I would like all derived objects to have the ability to include开发者_StackOverflow中文版. However, they don't have to, and if it's not necessary for them to use it, I don't want them to be forced to declare it. To enforce this, I've left the default behavior empty. For example, here's my whole sub:
Protected Overridable Sub MySubroutine(ByVal someObject As Object)
End Sub
Is this bad practice? I don't want to declare it as MustOverride
, but I don't want default behavior. Is there a "better" way of doing this, or is this perfectly acceptable? I don't want to be that hack programmer... just trying to learn :)
Yes, this is perfectly acceptable. A MustOverride
member can often be too strict of a requirement for an inheriting class so if you want a virtual member without default implementation this is the proper thing to do.
Anyone who is inheriting from this class will appreciate the fact that you have defined a virtual member as a "hook" into your class like this. Also the fact that you have declared this method as Protected
is also a good idea as public virtual members can be problematic from a maintenance standpoint.
As a step in your learning process, you should download .Net Reflector (from redgate).
Using it will tell you a lot about how others develop their code, and how Microsoft develops the framework code. You'll learn a lot from it.
精彩评论