C# - 调用基类中的方法
我有 2 个类:
public class A
{
public void WriteLine(string toWrite) { Console.WriteLine(toWrite); }
}
public class B : A
{
public new void WriteLine(string toWrite) { Console.WriteLine(toWrite + " from B"); }
}
在我的代码中,我执行以下操作:
B writeClass = new B();
writeClass.WriteLine("Output"); // I expect to see 'Output from B'
A otherClass = (A)writeClass;
otherClass.WriteLine("Output"); // I expect to see just 'Output'
我认为这会起作用,因为 多态性。
但是,它每次总是写“Output from B”。有没有办法让它按照我想要的方式工作?
编辑修复代码示例。
I have 2 classes:
public class A
{
public void WriteLine(string toWrite) { Console.WriteLine(toWrite); }
}
public class B : A
{
public new void WriteLine(string toWrite) { Console.WriteLine(toWrite + " from B"); }
}
In my code I do the following:
B writeClass = new B();
writeClass.WriteLine("Output"); // I expect to see 'Output from B'
A otherClass = (A)writeClass;
otherClass.WriteLine("Output"); // I expect to see just 'Output'
I presumed this would work because of polymorphism.
However, it always writes 'Output from B' every time. Is there anyway to get this to work the way I want it to?
EDIT Fixing code example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您使用 NEW 从基类“隐藏”方法时,您只是隐藏它,就是这样。当您显式调用基类实现时,它仍然会被调用。
A 不包含 WriteLine,因此您需要修复它。当我修复它时我得到了
When you "hide" a method from the base class using NEW you are just hiding it, thats it. It's still called when you explicitily call the base class implementation.
A doesnt contain WriteLine so you need to fix that. When I fixed it I got
A 类的方法是 Write,而不是 WriteLine。将其更改为相同的名称,它将按您的预期工作。我刚刚尝试了一下并得到:
多态性(C# 编程指南)很好地解释了这一点。 (这是原始海报链接的较新版本。)该页面显示了派生类重写虚拟成员以及新成员隐藏基类成员的示例。
新的修饰符似乎存在一些混乱。来自文档:
请注意,隐藏成员不必是虚拟的。
最佳实践:
Your method on class A is Write, not WriteLine. Change it to the same name and it will work as you expect. I just tried it and get:
Polymorphism (C# Programming Guide) explains this quite well. (This is the newer version of the original poster's link.) The page shows examples where a derived class overrides a virtual member and where new members hide base class members.
There appears to be some confusion over the new modifier. From the documentation:
Note that the hidden member does not need to be virtual.
Best practices:
重写类
B
中的方法时,请勿使用new
关键字。并将A
中的方法声明为virtual
。Don't use
new
keyword when you override the method in classB
. And declare the method inA
asvirtual
.'new' 关键字使 B 的 WriteLine 实现覆盖 A 的实现。
不要接受这个答案,但根据我的经验,以这种方式使用“new”关键字几乎总是错误的。代码的可读性较差,而且代码的清晰度也很混乱。
The 'new' keyword is making B's implementation of WriteLine overwrite A's implementation.
Don't accept this as an answer, but in my experience, it's almost always a mistake to use the 'new' keyword in this fashion. It's less readable and muddy's the clarity of your code.
你的类 A 有一个 Write 函数而不是 WriteLine
Your class A has a Write function instead of WriteLine
第一:我猜你想将这两个方法命名为“WriteLine”,但 A 类中的方法仅命名为“Write”。第二:是的,你从 A 继承了 B,但对象仍然是“B”类型,所以现在我认为你想要的不可能。
First: I guess you wanted to name the methods both "WriteLine" but the one in class A is only named "Write". And second: yes you inherit B from A but the object will still be of type "B" so now I don't think what you want is possible.