- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道以下行为:
public class A {
public virtual void Do() { Console.WriteLine("A"); }
}
public class B : A {
public override void Do() { Console.WriteLine("B override"); }
public void Do(int value = 0) { Console.WriteLine("B overload"); }
}
class Program {
public static void Main() {
new B().Do(); // ---> Console: "B overload"
}
}
我希望具有确切签名的重载优先于具有可选参数的另一个重载:我希望控制台中显示“B override”。相反,程序将“B 重载”写入控制台。
甚至 resharper 也失败并落入陷阱:
... Resharper 说带有可选参数的重载被带有精确签名的重载隐藏了,但实际上恰恰相反。
现在,如果您删除继承,那么它会按预期运行并且 resharper 警告是合法的:
public class B {
public virtual void Do() { Console.WriteLine("B override"); }
public void Do(int value = 0) { Console.WriteLine("B overload"); }
}
class Program {
public static void Main() {
new B().Do(); // ---> Console: "B override"
}
}
那么问题来了:解释这个观察结果的优先规则是什么?为什么具有精确签名的重载不优先于具有可选参数的另一个重载,以防具有精确参数的重载覆盖基本实现?
最佳答案
Why an overload with exact signature doesn't have precedence over another overload with optional parameters in case that the overload with exact parameters overrides a base implementation?
基本上这是遵循规范规则的编译器,尽管在这种情况下它们令人惊讶。 (第 7.6.5.1 节是 C# 5 规范的相关部分。)
编译器首先查看“最深”类型 - 即具有目标编译时类型的类型(在本例中为 B
)并尝试找到适用的函数成员 忽略任何重写基类中声明的方法:
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.
和:
The intuitive effect of the resolution rules described above is as follows: To locate the particular method invoked by a method invocation, start with the type indicated by the method invocation and proceed up the inheritance chain until at least one applicable, accessible, non-override method declaration is found. Then perform type inference and overload resolution on the set of applicable, accessible, non-override methods declared in that type and invoke the method thus selected. If no method was found, try instead to process the invocation as an extension method invocation.
因此在您的情况下,编译器仅 考虑新引入的方法,发现它适用(使用默认参数值)并停止搜索。 (即它不查看基类中声明的方法)。此时,候选函数成员集只有一个条目,因此此时没有真正的重载决议要执行。
我有一个 article on overloading它显示了这种情况,不是使用可选参数而是使用不同的参数类型 - 请参阅“继承”部分。
关于c# - 可选参数 : reversed precedence if override of overload exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29392335/
有没有在OpenJDK 1.7.0_45中派生类重写基类方法,但没有@Override注解,运行时派生类实例调用基类方法的情况? class Base { public f() { }
我正在尝试在 C++ 中练习 OOP,但我遇到了有关覆盖函数的问题。在我的 Shape2D 和 Shape3D 类中,我有在 Square 和 Sphere 类(分别为 ShowArea() 和 Sh
我想控制值在槽中的保存方式以及读取槽时返回的内容。这是我的类定义: (defclass object () ((name :accessor name-access :initf
我正在尝试在 C++ 中练习 OOP,但我遇到了有关覆盖函数的问题。在我的 Shape2D 和 Shape3D 类中,我有在 Square 和 Sphere 类(分别为 ShowArea() 和 Sh
我读了section在 Scala 编程中,引入了抽象重写,但我仍然对这些修饰符的连接到底意味着什么感到困惑。使用这些修饰符的代码片段粘贴在下面: trait Doubling extends Int
阅读Javadoc对于 @Override 注释,我遇到了以下规则: If a method is annotated with thisannotation type compilers are r
我正在基于 BEP20Token 模板 (https://github.com/binance-chain/bsc-genesis-contract/blob/master/contracts/bep
关于下面提到的 3 份契约(Contract): 1) Whenever hashCode() is invoked on the same object more than once during
在 C# 中,override 默认启用,那么,是否不需要显式在基类中将方法声明为可覆盖?如果是的话 Overridable 仅限于 VB.NET 还是在 C# 中也是必需的? 因此可以覆盖哪些类型的
以下代码在 public void onClick 行生成此错误消息。 Multiple markers at this line - implements android.view.View.OnC
当我在运行 IIS 的服务器 2012R2 上托管它时,我能够使用 Autorest 和我的 api 生成代码 但是,当我尝试使用 localhost url 运行它时,我收到一条无法读取的消息。 我
代码如下。 IDE 的代码没问题,但 gradle 拒绝构建,并表示: TextAdapter is not abstract and does not override abstract metho
这个问题已经有答案了: Best practice for overriding classes / properties in ExtJS? (3 个回答) 已关闭 8 年前。 这两个覆盖有什么区别
我今天将 xcode 更新为 7。更新后,我正在处理的项目出现警告“覆盖成员函数但未标记为‘覆盖’”。由于我们的项目将“踩踏警告为错误”设置为true。我遇到了很多错误。 我仔细检查了“Other L
我试图将 Apple 的 ARKit 示例应用程序集成到我的应用程序中。由于 ARKit 只是一个附加功能,所以我需要支持较低版本的 iOS。我在所有 ARKit 示例应用程序类中添加了 @avail
我覆盖了类的 Equals() 来比较 Guid 类型的 ID 值。 然后 Visual Studio 警告: ... overrides Object.Equals(object o) but do
我正在尝试用 Java 中的 Runnable 对象创建一个基本线程。下面是我的代码: import java.lang.Thread; import java.lang.Runnable; publ
我有一个函数: int function(int a, int b = 1, int c = 2){ return a+b+c; } 我想将“c”变量的值设置为3,但不想设置“b”的值 在像
我正在尝试了解GAS的.code16行为。 在手册中,对于16位部分,对于32位操作数或指令,似乎会为指令编码生成一个66H操作数替代前缀。这是否意味着 .code16 movw %eax, %ebx
我正在尝试创建一个 JFrame,向 JFrame 添加一个 JLabel(image),但这需要我抛出 IOException,这会弄乱我的 main 方法中的 run() 。 谁能告诉我如何抛出异
我是一名优秀的程序员,十分优秀!