Easily learn the abstract classes of C

  • 2021-08-31 08:57:12
  • OfStack

There is a special base class in C #, that is, abstract classes. Sometimes, the base class is not associated with concrete things, but simply expresses an abstract concept that provides a common interface for its derived classes. For this reason, the concepts of abstract classes and abstract methods are introduced in C #.
Overview of abstract classes
An abstract class provides a common definition of a base class shared by multiple derived classes and can provide both abstract and non-abstract methods. Abstract classes cannot be instantiated, and their abstract methods must be implemented by derived classes through inheritance, so they cannot be used with the new keyword or sealed. If a derived class does not implement all abstract methods, the derived class must also be declared as an abstract class. In addition, the implementation abstraction method is implemented by override method.
Abstract classes use the abstract modifier, and there are several rules for the use of abstract classes:

1 Abstract classes can only be used as base classes for other classes and cannot be instantiated directly, and the new operator cannot be used for abstract classes. If abstract classes contain abstract variables or values, they are either null types or contain references to instances of non-abstract classes. 2 Abstract classes allow abstract members, although this is not required. 3 Abstract classes cannot be sealed at the same time. 4 If a non-abstract class derives from an abstract class, it must be overloaded to implement all inherited abstract members.

The abstract keyword is required when declaring abstract classes in C #, and the syntax format is:
Access modifier abstract class class name: base class or interface
{
Class members;
}
When an abstract class is declared, all but the abstract keyword, the class keyword, and the class name are optional.
Overview of abstract methods
Because the abstract class itself expresses abstract concepts, many methods in the class do not necessarily have a concrete implementation, but only set aside an interface to be overloaded by the derived class.
If the abstract modifier is added to a method declaration, this method is called an abstract method. If a method is also declared abstract, it is also a virtual method by default. In fact, the abstract method is a new virtual method, which does not provide a specific method implementer, while the non-virtual derived class requires that it provide its own implementation for the inherited virtual method through overloading, while the abstract method does not contain specific implementation content, so the executor of the method declaration only has a semicolon ";" . Users can only declare abstract methods in abstract classes. For abstract methods, the static or virtual modifiers can no longer be used, and the method is not implemented by any executable program, even if it is only a pair of braces with a semicolon ";", it is not allowed to appear, just give the prototype of the method.
There are several points to note when declaring abstract methods:

1 Abstract methods must be declared in abstract classes. 2 You cannot use the virtual, static, and private modifiers when declaring abstract methods.

The abstract method declaration introduces a new method, but does not provide an implementation of this method. Because the abstract method does not provide any actual implementation, the method body of the abstract method contains only one semicolon. When deriving a non-abstract class from an abstract class, it is necessary to override the abstract method in the flying abstract class to provide the implementation of the concrete abstract method, and use override keyword when overriding the abstract method.
Here are two examples:


<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace qq 
{ 
  public abstract class A// Abstract class  
  { 
    public abstract void add(int a, int b);// Abstract method  
  } 
  public class B : A// Derived class B 
  { 
    public override void add(int a, int b)// Override abstract methods  
    { 
      int sum = a + b; 
      Console.WriteLine(sum); 
    } 
  } 
  public class C : A// Derived class C 
  { 
    public override void add(int a, int b)// Override abstract methods  
    { 
      int sum = System.Math.Abs(a + b);// Take the absolute value of the sum of two numbers  
      Console.WriteLine(sum); 
    } 
  } 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      B p = new B(); 
      p.add(1,-2); 
      C q = new C(); 
      q.add(1,-2); 
      Console.ReadLine(); 
    } 
  } 
} 
</span> 

It is easy to know that the output is-1 and 1


<span style="font-size:18px;">using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace qq 
{ 
  public class abtext 
  { 
    public virtual void output() 
    { 
      Console.WriteLine(" This is a virtual method "); 
    } 
  } 
  abstract public class class1 : abtext 
  { 
    public abstract override void output(); 
  } 
  public class class2 : class1 
  { 
    public override void output() 
    { 
      Console.WriteLine(" This is 1 A new method "); 
    } 
  } 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      class2 c1 = new class2(); 
      c1.output(); 
      Console.ReadLine(); 
    } 
  } 
} 
</span> 

It is easy to know that the result is: this is a new method

In fact, the article is very short, but it covers a comprehensive content. Finally, it explains the abstract class of C # through two examples, hoping to help everyone learn C # programming.


Related articles: