C # abstract classes, sealing classes, and Class Members

Source: Internet
Author: User
Tags define abstract

1. abstract classes and Class Members

You can use the abstract keyword to create incomplete classes and class members that must be implemented in the derived class.

 

For example:

 

PublicAbstractClassA
{
// Class members here.
}

Abstract classes cannot be instantiated. Abstract classes are used to provide public definitions of the base classes that can be shared by multiple Derived classes. For example, a class library can define an abstract class as a parameter of multiple of its functions, and requiresProgramYou can use this library to create a derived class to provide your own class implementation.

Abstract classes can also define abstract methods. The method is to set the keywordAbstractAdd to the front of the return type of the method.

 

For example:

PublicAbstractClassA
{
PublicAbstractVoidDowork (IntI );
}

 

Abstract methods are not implemented, so the method definition is followed by a semicolon instead of a regular method block. The derived class of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can use the abstract method to override the virtual method.

For example:

// Compile with:/Target: Library
Public ClassD
{
PublicVirtualVoidDowork (IntI)
{
// Original implementation.
}
}

PublicAbstractClassE: d
{
PublicAbstract overrideVoidDowork (IntI );
}

Public ClassF: E
{
PublicOverrideVoidDowork (IntI)
{
// New implementation.
}
}

IfVirtualThe method declaration isAbstractThis method is still a virtual method for all classes inherited from the abstract class. Classes that inherit abstract methods cannot access the original implementation of this method. In the previous exampleDoworkCannot callDowork. In this case, the abstract class can force the derived class to provide a new method implementation for the virtual method.

 

2. Sealing Class and Class Members

You can use the sealed keyword to avoid inheriting the class or some class members previously marked as virtual.

For example:

PublicSealedClassD

{

// Class members here.
}

 

The sealing class cannot be used as the base class. Therefore, it cannot be an abstract class. The seal class cannot be derived. Since the seal class is never used as the base class, some runtime optimization can make the call to the seal class members slightly faster.

 

The class member, method, field, attribute, or event on the derived class that overwrites the Virtual Member of the base class can be declared as a sealed member. When used in a later derived class, this will cancel the virtual effect of the member. The method is to place the sealed keyword in the class member declaration

Before the override keyword. For example:

Public ClassD: c
{
PublicSealed overrideVoidDowork (){}
}

See http://msdn.microsoft.com/zh-cn/library/kx37x362.aspx

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.