Abstract class of C #

Source: Internet
Author: User
Tags abstract

Sometimes a base class is not associated with a specific object, but rather an abstract concept that provides a common interface for its derived classes. To this end, C # introduces the concept of abstract classes (abstract class).

Note: C + + programmers are most likely to make mistakes here. There is no method for directly declaring abstract classes in C + +, and it is thought that the class is an abstract class as long as the pure virtual function is defined in the class. Pure virtual function of the concept is more obscure, intuitive and not easy for people to accept and master, so C # abandoned this concept.

Abstract classes Use the abstract modifier, there are several provisions for the use of abstract classes:

An abstract class can only be the base class for other classes, it cannot be instantiated directly, and you cannot use the new operator for an abstract class. Abstract classes, if they contain abstract variables or values, are either null types or contain references to instances of non-abstract classes.

Abstract classes allow abstract members to be included, although this is not required.

Abstract classes cannot be sealed at the same time.

If a non-abstract class derives from an abstract class, it must implement all inherited abstract members by overloading, see the following example:

Abstruct class A
{
Public abstruct void F ();
}
Abstract class B:a
{
public void G () {}
}
Class C:b
{
public override void F () {
The specific implementation code of F
}
}
Abstract Class A provides an abstract method F. Class B inherits from abstract class A and provides a method G, because B does not contain an implementation of F, so B must also be an abstract class. Class C is inherited from Class B, an abstract method F is overloaded in a class, and a concrete implementation of f is provided, and Class C allows non-abstract.

Let's inherit the example of a car class. We understand the vehicle class from the angle of "transportation", it should express an abstract concept, we can define it as abstract class. This abstract class is inherited by car class cars and truck class truck as a class that can be instantiated.

Program Listing 14-5:

Using System;
Abstract class Vehicle//define auto class
{
  public int wheels;//Publicly owned member: number of wheels
  protected float weight;//Protect Members: Weight
  Public Vehicle (int w,float g) {
     wheels=w;
     weight=g;
  }
  public virtual void Speak () {
     Console.WriteLine ("The W Vehicle is speaking!");
  }
;

Class Car:vehicle//Definition Sedan class
{
  int passengers;//Private Member: Number of passengers public car
  (int w,float g,int p): Base (w,g)
  {
    wheels=w;
    weight=g;
    passengers=p;
  }
  public override void Speak () {
    Console.WriteLine ("The ' car is speaking:di-di!")
   ;
}

Class Truck:vehicle//Definition Truck class
{
  int passengers;//Private Member: Number of passengers
  float load;//Private Member: Load Public
  truck (int w , float g,int p,float L): Base (w,g)
  {
    wheels=w;
    weight=g;
    passengers=p;
    load=l;
  }
  public override void Speak () {
    Console.WriteLine ("The Truck is speaking:ba-ba!");
  }
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.