C # For interface implementation, display implementation interface, and inheritance,

Source: Internet
Author: User

C # For interface implementation, display implementation interface, and inheritance,

First, list the code I wrote:

Interfaces, abstract classes, and implementation classes

  public interface IA    {        void H();    }    public interface IB    {        void H();    }    public abstract class D    {        public abstract void H();    }    public class C : D,IA, IB    {        void IA.H()        {            Console.WriteLine("all a.h");        }        public override void H()//T        {            Console.WriteLine("all b.h");        }    }

If Class C inherits the abstract class D, you can use the override keyword in Class C. The method H (T location) that the interface IB calls is also overwritten) [I can understand the T-position method. H colleagues overwrite method H in abstract class D and method H in interface IB ].

If Class C does not inherit the abstract class D, the override keyword cannot be used in Class C. The override keyword can only be used when the abstract class is inherited (this is what I feel after using it ).

The initial code is as follows:

    public interface IA    {        void H();    }    public interface IB    {        void H();    }    public abstract class D    {        public abstract void H();    }    public class C : D,IA, IB    {        public override void H()        {            Console.WriteLine("all h");        }        void IA.H()        {            Console.WriteLine("all a.h");        }        void IB.H()        {            Console.WriteLine("all b.h");        }    }

Display implementation interface. When displaying the implementation interface, you cannot use the access permission keyword [private, protected, public, etc.] On the covered methods or fields]

 

If the abstract class D is not inherited:

    public class C : IA, IB    {        public void H()//U        {            Console.WriteLine("all h");        }        void IA.H()        {            Console.WriteLine("all a.h");        }        void IB.H()        {            Console.WriteLine("all b.h");        }    }

During the call, the interface IA object can only access IA. H (), interface IB can only access IB. H (), and the method that cannot access the U location. The method H at the U location can be accessed only when class C is instantiated.

 

 

 

Call code:

   class Program    {        static void Main(string[] args)        {            IA a = new C();            IB b = new C();            a.H();            b.H();            D d = new C();            d.H();            C c = new C();            c.H();            Console.WriteLine("Hello World!");            Console.ReadLine();        }    }

 

The above is my personal summary. If you have any mistakes, please correct them. Your language is not good. Welcome to criticism.

 

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.