C # Supplements's trivia (iv): inheritance

Source: Internet
Author: User
Method overrides:

The base class method identifies the virtual keyword, the subclass (inheriting Class), in the method override to identify the override keyword.

The overridden method must be the same as the type of the base class, such as the method name, return, and accept parameters.

  public class Class1    {public        virtual void Show (int i)        {...}    }    public class Class2:class1    {public        override void Show (int a)        {...}    }

The new method overrides:

    public class Class1    {        protected virtual void Show (int i)        {...}    }    public class Class2:class1    {public        new int show (int a)        {...}    }

The new declaration defines its own method with the same name. The return type, access type can be different from the base class with the same name method.

If the accept parameter differs from the base class method with the same name, the new keyword is redundant, which is equivalent to a general override (the base class has no same method, and no hidden method).

Override the method body that overrides the base class method (hidden method), and new overrides the base class with the same name method (the receive parameter is the same).

Terminating inheritance:

Terminates the inheritance identity sealed. The following code compiles an error and Class2 cannot inherit the enclosing class Class1

public sealed class Class1    {public        virtual void Show (int i)        {...}    }    public class Class2:class1    //{    //public    override void Show (int a)    //    {...}    //}

Call the base class constructor:

The compiler calls the base class constructor first, and then calls the inheriting class constructor.

  public class Class1    {public        Class1 (int i, string j)        {...}    }    public class Class2:class1    {public        Class2 (int i,string j,object obj): Base (i,j)        {...}    }

The above is the C # supplements of the Small Knowledge (iv): Inherited content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.