- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
(又是一个恼人的问题...)
询问后this 之前-(这部分与我的问题有关)-我得到了答案:
See §7.6.5.1 of the C# 4 spec:
The set of candidate methods is reduced to contain only methods from the most derived types: For each method C.F in the set, where C is the type in which the method F is declared, all methods declared in a base type of C are removed from the set.
好的。
我有这段代码://.Dump() 就像一个 WriteLine 命令...
public class Base
{
public void Foo(string strings) { "1".Dump();}
public virtual void Foo(object strings) { "2".Dump();}
}
public class Child : Base
{
public override void Foo(object strings) { "4".Dump();}
}
但是这段代码:
Child c = new Child();
c.Foo("d");
发出:“1”
但是等等......
我们不是说 被简化为只包含来自大多数派生类型的方法:
吗?
child 有 1 个来自其父亲的函数 public void Foo(string strings)
和一个 NEARER 覆盖函数。
那么他为什么选择它的基地功能呢?继承的函数是否比覆盖更近?
是否与运行时覆盖有关?
请帮忙。
这种情况呢?
public class Base
{
public virtual void Foo(int x) { "1".Dump();}
}
public class Child : Base
{
public override void Foo(int x) { "2".Dump();}
public void Foo(object x) { "3".Dump();}
}
void Main()
{
Child c = new Child();
c.Foo(6); //emits "3"
}
最佳答案
那是因为您的 child 拿走了一个物体。
public class Child : Base
{
public override void Foo(object strings) { "4".Dump();}
}
将其设为字符串,然后调用子节点。
public class Child : Base
{
public override void Foo(string strings) { "4".Dump();}
}
为什么会这样?
因为编译器发现 child 有 object
参数,所以它必须转换为 string
而在基类中它很容易作为字符串使用。
所以它调用了 base one。
虽然覆盖函数更接近于子类。但是这里的规则在 child 和基础上是不同的。 Child 有对象,base 有字符串。如果两者都有 object
或者都有 string.
我在 Jon Skeet 的 C# in Depth Overloading Section 中读到了这个
关于c# - 派生方法比 C# 中的覆盖更强大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10780883/
Stackoverflow JS Genius的! 我当前的项目有一个问题,它正在使用 Node 的HTTP createServer,并使用Formidable来解析主体数据。 请参阅下面的代码。
我正在尝试将文件上传到我的 NodeJS 服务器。我目前收到错误: 错误:错误的内容类型 header ,未知的内容类型:image/jpeg “image/jpeg”是有效的 MIME 类型,为什么
我有一个用作路由器的 Node.JS 服务器,可以向它发出 post 请求以上传文件,应该只允许 jpg/png/jpeg 扩展名我目前正在做的是: var form = new formidable
我正在使用https://github.com/felixge/node-formidable用于使用express上传文件。 虽然它工作得很好,但有一个问题:我有一个包含 9 个 type="fil
Haskell 的类型推理引擎比 Scala 的类型推理引擎强大得多。在 Haskell 中,我很少需要显式编写类型,而在 Scala 中,类型只能在表达式中推断,而不能在方法定义中推断。 例如,请参
我将 Superpowered 用于各种实时 FX,它们都非常简单。然而,音高变换是另一回事,我认为事实上是因为它基于时间拉伸(stretch)算法,当然必须处理随时间变化的输出,这比应用 EQ 或混
我正在使用mean stack 和formidable上传文件 表单具有 multipart/form-data 属性 exports.create = function(req, res) {
我正在尝试构建我的第一个 node.js 应用程序,但是,使用 node server.js 运行时,我收到以下错误消息: connect: multipart: use parser (multip
对于强大的 npm 包,当我使用 import * as formidable from "formidable" 时,我收到一条错误消息,指出 formidable({ multiples: tru
我注意到了 std::string str; str += 'b'; // works str.append('b'); // does not work str.append(1, 'b'); //
我有一个需要访问本地 FS 的 HTML/JS(YUI 框架)照片管理器。我应该将 HTML/JS 移动到 AIR,还是硬着头皮将其“移植”到 Flex AIR? 我知道营销说的是什么,但我想要真正的
我是一名优秀的程序员,十分优秀!