The base class explicitly inherits the interface. When the class inherits the base class, it inherits the same interface, leading to confusion in interface methods (disadvantages of the explicit inheritance interface)

Source: Internet
Author: User

The base class baseoutput explicitly inherits an interface ioutput, and the class trackoutput inherits baseoutput, and also inherits the ioutput interface. Assume that ioutput has a method output, so there are two output methods in trackoutput, one source is the base class baseoutput and the other source is the interface ioutput. This leads to confusion. What should I do? Let's take a look at the following 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();             }    }
During compilation, base. output reports an error indicating that baseoutput does not contain the output definition. How can this happen? Does baseoutput inherit the ioutput method and implement the output method in ioutput?

If you explicitly implement the output in ioutput in baseoutput, change it to implicit implementation. The new Code is as follows:

  class BaseOutput : IOutput    {         public void output()        {            Console.WriteLine("IOutput...");        }       }
In this way, the compilation can be passed and the test code is as follows:

static void Main(string[] args)        {                    TrackOutput t = new TrackOutput();            t.Output();            Console.ReadLine();        }

The output is normal. Therefore, the explicit implementation of interfaces may cause problems during secondary inheritance.

What if we need to explicitly implement ioutput in baseoutput and also implement output in trackoutput? Two solutions can be considered.

Solution 1:

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();            }    }


We can see that the result is as expected. However, it is worth noting that after ioutput I = this conversion, I will call the output method of trackoutput, and after calling the output method, it also triggers ioutput I = this conversion, and then loops continuously, leading to infinite recursion, leading to problems.

To solve this problem, I have solution 2.

Solution 2: add an output virtual method to the base class baseoutput, and then reload it 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();             }    }
The test results are as follows:



Conclusion: When you explicitly inherit an interface, If You Need Secondary inheritance, add a virtual method to the base class. Otherwise, implicit inheritance is used.

Reprinted please indicate the source: http://blog.csdn.net/xxdddail/article/details/39393489





The base class explicitly inherits the interface. When the class inherits the base class, it inherits the same interface, leading to confusion in interface methods (disadvantages of the explicit inheritance interface)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.