C # Design pattern-derived classes implement instance code sharing for non-virtual interface traps

Source: Internet
Author: User

Understand the difference between an interface method and a virtual method

At first glance, there seems to be no difference between implementing an interface and covering a virtual method, in fact, the difference between implementing an interface and covering a virtual method is great!!!

Derivation cannot overwrite non-virtual members of an interface

A member method declared in an interface is not a virtual method by default, so a derived class cannot overwrite a non-virtual member that implements an interface in a base class .
See an example.
Define the interface ITest:

    Public interface ITest    {        void Test ();    

Implements the base class and the derive class for the interface

    public class Base:itest    {public        Base ()        {            Console.WriteLine ("This is Base constructor");        }        Implement the ITest interface public        void Test ()        {            Console.WriteLine ("This is in base to ITest implement");}    }    public class Derive:base,itest    {public        Derive ()        {            Console.WriteLine ("This is derived Constructor ");        }        Does the test derive class implement ITest?? Public        void Test ()        {            Console.WriteLine ("Derive to ITest Implement");}    }

Call object base and derive:

            Base B = new Base ();            B.test ();            Base d = new Derive ();//Declare D as Base Object            d.test ();            Console.ReadLine ();

The output is:



As you can see, the test method implementation of the B and D instances is in the base class!!! This indicates that a derived class cannot overwrite a member (not a virtual method) that implements an interface in a base class .

However, take a look at the following call:

            Base B = new Base ();            B.test ();            Derive d = new Derive (); Declare D as Derive object            d.test ();            Console.ReadLine ();

The output is:



Therefore, if an object that you want to inherit calls an interface implementation method, it can only be declared as an derive instance. Doing so does not conform to the principle that variables are declared as base instances in effective C # !!!

Derived class method personality, convert base class method to virtual method

To avoid this confusion of use, if it is true that the derived class implementation method is a personality behavior, then the method of implementing the interface of the base class needs to be pre-added with the virtual modifier!
The code is modified as follows:

public class Base:itest    {public        Base ()        {            Console.WriteLine ("This is Base constructor");        }        public virtual void Test ()//implements the virtual method of the ITest interface        {            Console.WriteLine ("This was in base to ITest implemnt");}    } Public    class Derive:base,itest    {public        Derive ()        {            Console.WriteLine ("This is derived constructor");        }         public override void Test ()//Implement the replication method for interface ITest        {            Console.WriteLine ("This was in Derive to ITest implemnt");        }    }

Once implemented, multiple associated objects use

Observing the above code, we found that the interface implemented in the base class, if the derived class would also like to implement this interface, then it inherits the interface implementation of the base class by default, so it is not necessary to implement the interface with repeated write code.

   Public interface ITest    {        void Test ();    public class Base:itest    {public        Base ()        {            Console.WriteLine ("This is Base constructor");        }        public void Test ()        {            Console.WriteLine ("This was in base to ITest implemnt");        }    }    public class Derive:base,itest    {public        Derive ()        {            Console.WriteLine ("This is derived Constructor ");        }    }

Summarize:
1, the derivation can not overwrite the non-virtual members of the interface;
2, if the derived class method is a personality method, the base class method is converted to virtual method;
3, the Destructors class implements the interface method, the derived class also shows the inheritance of this interface, but do not have to implement again!!!

Related Article

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.