Abstract methods of C #

Source: Internet
Author: User
Tags abstract definition execution

Because abstract classes themselves express abstract concepts, many of the methods in a class do not necessarily have a specific implementation, but simply set aside an interface to overload the derived class. To give a simple example, the class "graph" is abstract, and its member method "compute the plot area" also has no practical meaning. The area only works on the non-abstract concepts of "graphics" such as "circle" and "triangle", then we can declare the member method of the base class "compute area" as abstract, and the concrete implementation is implemented by the derived class by overloading.

If the abstract modifier is added to a method declaration, we call the method abstract (abstract method).

If a method is declared to be abstract, then the method defaults to a virtual method. In fact, an abstract method is a new virtual method that does not provide a specific method to implement the code. We know that a non-virtual derived class requires that an inherited virtual method provide its own implementation by overloading, whereas an abstract method does not contain specific implementation content, so there is only one semicolon "in the execution body of the method declaration";

Abstract methods can only be declared in an abstract class. For an abstract method, you can no longer use static or virtual modifiers, and the method cannot have any executable code, even if it is just a pair of curly braces with a semicolon "{;}" Are not allowed to appear, only need to give a prototype of the method can be.

The "Siren" method of "transportation" is actually meaningless, and we're going to use the concept of abstract methods to continue to rewrite examples of car classes:

Program Listing 14-6:

Using System;
Abstruct class Vehicle//define auto class
{
  public int wheels;//Publicly owned members: number of wheels
  protected float weight;//Protect Members: Weight
  Public Vehicle (int w,float g) {
     wheels=w;
     weight=g;
  }
 public abstract void Speak ();

Class Car:vehicle//Definition Sedan class
{
  int passenger;//Private Member: Number of passengers public car
  (int w,float g,int p): Base (w,g)
  { C16/>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!");
   }

Also note that abstract methods cannot be accessed using the base keyword in a derived class. For example, the following code will have an error at compile time:

Class A
{
public abstract void F ();
}

Class B:a
{
public override void F () {
Base. F (); Error, base. F is an abstract method
}
}
We can also use abstract methods to overload the virtual methods of the base class, when the execution code of the virtual method in the base class is "blocked". The following examples illustrate this point:

Class A
{public
 virtual void F () {
    Console.WriteLine ("A.F");
 }

Abstract class B:a
{public
 abstract override void F ();
}

Class C:b
{public
 override void F () {
  Console.WriteLine ("C.F");
 }

Class A declares a virtual method F, and derived Class B overloads the F with an abstract method so that B's derived class C can overload F and provide its own implementation.

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.