C #: abstract classes and Abstract Functions, sealing classes and sealing methods

Source: Internet
Author: User

1. abstract classes and Abstract Functions

  • Use the abstract keyword in C # To declare classes and functions as abstract classes or abstract functions.
  • Abstract classes cannot be instantiated. Abstract functions do not execute code and must be rewritten in non-Abstract Derived classes.
  • Abstract Functions are virtual, but virtual keywords are not required. Otherwise, an error is returned.
  • If a class contains an abstract letter, the class is also abstract and must be declared as abstract.
Abstract class myabstract {// variable definition does not apply abstract keywords private int I; // attribute public abstract int count {Get; Set ;}// method public abstract string getname ();}

2. sealing and sealing methods

  • Use the keyword sealed in C # To declare classes and methods as sealing classes or sealing methods.
  • For a sealed class, this class cannot be inherited. For a sealed method, this method cannot be rewritten.
  • A sealed class cannot be inherited as a base class, but it can inherit other classes or excuses.
  • Protected or virtual members cannot be declared in the seal class, because the protected member can only be accessed from the derived class, and the virtual member can only be rewritten in the derived class
  • Because the sealing class cannot be inherited, the sealing class cannot be declared as abstract, that is, the sealed modifier cannot be used together with the abstract modifier.
1 Public sealed class myclass // declare the sealing Class 2 {3 Public Int = 0; 4 Public void method () 5 {6 console. writeline ("sealing class"); 7} 8}
  • Not every method can be declared as a sealing method. The sealing method can only be used to implement the virtual method of the base class and provide specific implementation. Therefore, when declaring the sealing method, sealed is always used together with override
Using system; namespace consoleapplication2 {class program {static void main (string [] ARGs) {myclass2 test = new myclass2 (); test. method () ;}} public class myclass1 {Public Virtual void method () {console. writeline ("Virtual Methods in the base class"); console. readline () ;}} public sealed class myclass2: myclass1 {public sealed override void method () {base. method (); console. writeline ("method after rewriting in the seal class ");}}}

 

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.