可能重複:
C# 4: conflicting overloaded methods with optional parametersC#與接口重載方法行爲
我只是有一個小的研究和創造下一個代碼。
namespace Test { class Program { public interface ITestA { void MethodA(int a, int b); } public class TestAClass : ITestA { public void MethodA(int a, int b) { Console.WriteLine("MethodA with param"); } public void MethodA(int a, int b, bool logic = true) { Console.WriteLine("MethodA logic with param"); } } public interface ITestB { void MethodA(int a, int b, bool logic = true); } public class TestBClass : ITestB { public void MethodA(int a, int b) { Console.WriteLine("MethodB with param"); } public void MethodA(int a, int b, bool logic = true) { Console.WriteLine("MethodB logic with param"); } } static void Main(string[] args) { var testA = new TestAClass(); testA.MethodA(1, 1); var testB = new TestBClass(); testB.MethodA(1, 1); } } }
我有一個問題,爲什麼編譯器總是選擇短的方法有兩個參數。當然,所有這些工作都是以相同的方式進行,沒有Interface。
由於
因此,如果它的工作方式與使用和不使用界面相同,爲什麼在您的問題中顯示界面?這個想法是使用*最少量的代碼來重現行爲。這讓我們專注於實際問題,而不是不相關的細節。 – 2011-05-16 12:25:06
此外,這是肯定**不**操作符重載,而是*方法*重載,完全不同的事情。 – 2011-05-16 12:25:49
主要問題是可選參數,而不是接口。 – Rik 2011-05-16 12:29:50