Small knowledge (4): inheritance, small knowledge inheritance

Source: Internet
Author: User

Small knowledge (4): inheritance, small knowledge inheritance

Method Rewriting:

The virtual keyword of the base class method identifier, and the override keyword of the method rewriting identifier in the subclass (inheritance class.

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

Public class Class1

{

PublicVirtualVoidshow (int I)

{......}

}

Public class Class2: Class1

{

PublicOverrideVoidshow (int)

{......}

}

Rewrite the New method:

Public class Class1

{

ProtectedVirtualVoidshow (int I)

{......}

}

Public class Class2: Class1

{

PublicNewIntshow (int)

{......}

}

The new statement defines a method with the same name. The return type and access type can be different from the same name methods of the base class.

If the accept parameter is different from the method with the same name as the base class, the new keyword is redundant, which is equivalent to General rewriting (the base class does not have the same method, and there is no same hidden method ).

Override RewritingBase Class Method(Hidden method)Method body, New rewriteMethod with the same name as the base class(Receive parameters are the same ).

Terminate inheritance:

End the inheritance identifier sealed. The following code compilation error occurs. Class2 cannot inherit the closed class Class1.

Public sealed class Class1

{

PublicVirtualVoidshow (int I)

{......}

}

// Public class Class2: Class1

//{

// PublicOverrideVoidshow (int)

//{......}

//}

Call the base class constructor:

The compiler first calls the base class constructor and then calls the inherited 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)

{......}

}

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.