The base class Baseoutput explicitly inherits an interface Ioutput, after which the class Trackoutput inherits Baseoutput, and the Ioutput interface is inherited at the same time. Suppose Ioutput has a method output, so there are two output methods in Trackoutput, one from the base class Baseoutput, and one originating from the interface ioutput. This has led to confusion. What to do? Let's look at the following section of code
Interface Ioutput { void output (); }
Class Baseoutput:ioutput { void Ioutput.output () { Console.WriteLine ("Ioutput ...");} } Class Trackoutput:baseoutput, Ioutput {public void Output () { Console.WriteLine ("Trackoutput ..."); Base. Output (); } }
At compile time. Base. Output will error, indicating that the definition of output is not included in Baseoutput. How did this happen? Baseoutput not inherit the Ioutput method, and implement the method of output in Ioutput?
Assume that the output in the Baseoutput is explicitly implemented in the Ioutput. Changed to an implicit implementation. The new code such as the following:
Class Baseoutput:ioutput {public void output () { Console.WriteLine ("Ioutput ...");} }
This will allow the compilation to pass. And can also perform normally, test code such as the following:
static void Main (string[] args) { Trackoutput t = new trackoutput (); T.output (); Console.ReadLine (); }
The output is normal. Therefore, the explicit reality of the interface is now two times the inheritance, there will be a failure.
What if you need to explicitly implement Ioutput in Baseoutput, and you need to implement output in Trackoutput? Two scenarios can be considered.
Programme one:
In Trackoutput, it is explicitly converted to ioutput and then output. The code is as follows:
Class Baseoutput:ioutput { void Ioutput.output () { Console.WriteLine ("Ioutput ...");} } Class Trackoutput:baseoutput, Ioutput {public void output () { Console.WriteLine ("Trackoutput ..." ); Ioutput i = this; I.output (); } }
Can see that the result is as we expect the output. It's just that. One thing to note here is that after the conversion of Ioutput I=this. I call the output method again. The output method of the trackoutput is called. When the output method is called, the conversion of the Ioutput i=this inside is triggered, and then the loop continues, causing infinite recursion. This can cause problems.
In order to solve the problem, I have the option two.
Scenario Two: Add a virtual method of output in the base class Baseoutput. It is then overloaded in Trackoutput.
The code is as follows:
Class Baseoutput:ioutput { void Ioutput.output () { Console.WriteLine ("Ioutput ..."); } public virtual void output () { Console.WriteLine ("Myoutput ...");} } Class Trackoutput:baseoutput, Ioutput {public void output () { Console.WriteLine ("Trackoutput ..."); Base.output (); } }
Test results such as the following:
Summary: When explicitly inheriting an interface, assume that you need to inherit two times, and remember to add an implementation of a virtual method to the base class. Otherwise, implicit inheritance is used.
Reprint Please specify source: http://blog.csdn.net/xxdddail/article/details/39393489
The base class explicitly inherits the interface, which inherits the same interface when the class inherits the base class, causing the interface method to be confusing (the disadvantage of explicitly inheriting an interface)