从覆盖的方法阻止C#子 [英] Preventing a C# subclass from overwriting a method
问题描述
说我有一个名为父的抽象父类实现了一个名为DISPLAYTITLE的方法。我想这个方法是继承父每个子类相同的 - 如果一个子类,试图实现自己的DISPLAYTITLE的方法,我想编译错误。我如何在C#中做到这一点。我相信,在Java中,我只是标记方法,最终的,但我似乎无法找到在C#中的替代品。我一直在封闭和覆盖瞎搞,但我不能得到我要找的行为。
例如,在此代码:
使用系统;
命名ConsoleApplication1
{
级父
{
公共无效DISPLAYTITLE(){Console.WriteLine(父的标题); }
}
类ChildSubclass:家长
{
公共无效DISPLAYTITLE(){Console.WriteLine(孩子自己的标题的执行情况);
}
静态无效的主要(字串[] args)
{
ChildSubclass myChild =新ChildSubclass();
myChild.DisplayTitle();
到Console.ReadLine();
}
}
}
我想收到编译错误说,ChildSubClass无法覆盖DISPLAYTITLE。目前,我得到一个警告 - 但似乎这是一件我应该可以做的,我不知道正确的属性标记方法
如何在C#中做到这一点。我相信,在Java中,我只是标记方法,最终的,但我似乎无法找到在C#中的替代品。
块引用>
大致相当于为
密封
在C#中,但你通常只需要它的虚方法 - 和你的DISPLAYTITLE
方法不是虚拟的。
要注意这一点很重要
ChildSubclass
的不是的覆盖DISPLAYTITLE
- 它的隐藏的它。其中只使用引用父任何代码
不会最终调用该实现。
请注意,与代码原样,你应该得到一个编译时警告,提醒您对
新
修改添加到方法ChildSubclass
公开新无效DISPLAYTITLE(){...}
您不能的停止的隐藏,从现有的方法,除了通过密封类本身来防止派生类的创建完全派生类...但不直接使用派生类型不会在意。
呼叫者
什么是你的真正的关注吗? ?意外误用或故意的问题。
编辑:请注意,您的示例代码警告会是这样的:
test.cs中(12,19):警告CS0108:
'ConsoleApplication1.ChildSubclass.DisplayTitle()隐藏继承
会员'ConsoleApplication1.Parent.DisplayTitle( )。使用new关键字
如果隐藏之意。
我建议你把警告变成错误,然后它很难忽略它们:)
Say I have an abstract parent class called "Parent" that implements a method called "DisplayTitle". I want this method to be the same for each subclass that inherits "Parent" - I would like a compile error if a subclass attempts to implement their own "DisplayTitle" method. How can I accomplish this in C#. I believe in Java, I'd just mark the method as "final", but I can't seem to find an alternative in C#. I've been messing around with "sealed" and "override", but I can't get the behavior that I'm looking for.
For example, in this code:
using System; namespace ConsoleApplication1 { class Parent { public void DisplayTitle() { Console.WriteLine("Parent's Title"); } } class ChildSubclass : Parent { public void DisplayTitle() { Console.WriteLine("Child's Own Implementation of Title"); } static void Main(string[] args) { ChildSubclass myChild = new ChildSubclass(); myChild.DisplayTitle(); Console.ReadLine(); } } }
I'd like to receive a compile error saying that the "ChildSubClass" can't override "DisplayTitle". I currently get a warning - but it seems like this is something that I should be able to do and I don't know the proper attributes to label the method.
解决方案How can I accomplish this in C#. I believe in Java, I'd just mark the method as "final", but I can't seem to find an alternative in C#.
The rough equivalent is
sealed
in C#, but you normally only need it for virtual methods - and yourDisplayTitle
method isn't virtual.It's important to note that
ChildSubclass
isn't overridingDisplayTitle
- it's hiding it. Any code which only uses references toParent
won't end up calling that implementation.Note that with the code as-is, you should get a compile-time warning advising you to add the
new
modifier to the method inChildSubclass
:public new void DisplayTitle() { ... }
You can't stop derived classes from hiding existing methods, other than by sealing the class itself to prevent the creation of a derived class entirely... but callers which don't use the derived type directly won't care.
What's your real concern here? Accidental misuse, or deliberate problems?
EDIT: Note that the warning for your sample code would be something like:
Test.cs(12,19): warning CS0108: 'ConsoleApplication1.ChildSubclass.DisplayTitle()' hides inherited member 'ConsoleApplication1.Parent.DisplayTitle()'. Use the new keyword if hiding was intended.
I suggest you turn warnings into errors, and then it's harder to ignore them :)
这篇关于从覆盖的方法阻止C#子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!