Sealed keyword usage

Source: Internet
Author: User

Sealed is sealed. As the name suggests, when this keyword modifies a class, the sealing class cannot be inherited. The sealing method will override the methods in the base class, but it cannot be further rewritten in any derived class. When applied to methods or attributes,SealedThe modifier must always be used with override.

That is to say, to use sealed to modify the method, the method to be modified must be the method to rewrite the parent class. Sealed and override are used together. This sealed keyword is the same as the final keyword in Java. The class cannot be inherited, and the method cannot be overwritten. Because interfaces and abstract classes can only be inherited, the sealed keyword cannot be used to modify abstract classes and interfaces. Msdn also explains that the structure is implicitly sealed, which is why the structure cannot be inherited.

Public class person {string name; int age; Public Virtual void sayhello () {console. writeline ("I am person Class Object") ;}} public class Chinese: person {public sealed override void sayhello () // sealed keyword used with override, this method cannot be rewritten by the quilt class {console. writeline ("I am chinesese object") ;}} public sealed Class A: Chinese // This class cannot be inherited. {String school = "gdpu"; // public override void sayhello () {} rewrite error. This member is sealed and cannot be rewritten}

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.