If a class is not associated with a specific thing, but simply expresses an abstract concept, just as a base class for its derived class, such a class is an abstract class, and when you declare a method in an abstract class, it is an abstract method when you add abstract.
Abstract class overview and declarations
The main differences between abstract and non-abstract classes:
①. Abstract classes cannot be instantiated directly
②. Abstract classes can have only abstract methods, or only non-abstract methods, can also be abstract methods, non-abstract methods have. (There cannot be abstract methods in non-abstract classes).
③. Abstract classes cannot be sealed
The above will be verified in the following example one by one:
Class Program
{
static void Main (string[] args)
{
M m = new M ();//Error Hint: Cannot create an instance of an abstract class or interface M. Validation of ①, abstract classes and interfaces cannot be instantiated
。
}
}
Public abstract class M
{
public void A ()
{
Console.WriteLine ("A method called");
}
public abstract void B ()//Error Hint: M.B () cannot declare the principal because it is marked abstract. In other words, abstract parties
Law cannot have a method body.
{
Console.WriteLine ("called the B Method");
}
public abstract void G ();
}
public class C
{
public abstract void H ();//Error Hint: C.D () is abstract, but it is contained in non-abstract class C. Comparing the methods in M Class A (), B (), it verifies that ②: there is no abstract method in a non-abstract class, abstract methods can only be abstract, or only non-abstract methods, abstract methods, non-abstract methods are available.
public void D ()
{
}
}
public sealed abstract class E//Error hint: E is abstract, it cannot be sealed (sealed) or static. This verifies the ③.
{
}
public static abstract class F//Error Hint: F is an abstract class, it cannot be sealed (sealed) or static. This verifies the ③.
{
}
2. Summary and declaration of abstract methods
When declaring an abstract method, be aware that:
①. The abstract method must be declared in the abstract class (which has been verified above).
②. When declaring an abstract method, you cannot use the virtual, static, private modifier.
③. Abstract methods do not provide implementations in abstract classes.
The following example verifies that:
Class Program
{
static void Main (string[] args)
{
}
}
Public abstract class M
{
public abstract void A ();
Public virtual abstract void B ();//Error Hint: Abstract method B cannot be marked as virtual. Because virtual methods and abstract methods belong to the same hierarchical, parallel relationship.
public static abstract void C ();//Error Hint: Static member C cannot be marked as Overrid, virtual, or abstract.
Private abstract void D (); Error tip: Virtual or abstract members cannot be private. Because virtual members are inherited classes to override, if it is private, inheriting the class can not see, if the abstract method is private, this abstract method is the subclass of the abstract class can not implement this abstract method, invisible.
A ();//The abstract method cannot have a method body in its own abstract class, so it cannot be implemented in this class. To be implemented in an inheriting class.
}
3. Use of abstract classes and abstract methods
To give an example directly:
Using System;
Using System.Collections.Generic;
Using System.Text;
namespace _
{
Public abstract class MyClass
{
private string id = "";
private String name = "";
<summary>
Number attributes and implementation
</summary>
public string ID
{
Get
{
return ID;
}
Set
{
id = value;
}
}
<summary>
Name attributes and implementation
</summary>
public string Name
{
Get
{
return name;
}
Set
{
name = value;
}
}
<summary>
Abstract method, used to output information
</summary>
public abstract void Showinfo ();
}
public class Driveclass:myclass//Inherit abstract class
{
<summary>
overriding methods for outputting information in abstract classes
</summary>
public override void Showinfo ()
{
Console.WriteLine (ID + "" + Name);
}
}
Class Program
{
static void Main (string[] args)
{
Instantiating derived classes
Driveclass Driveclass = new Driveclass ();
Instantiating an abstract class using a derived class object
MyClass myClass = Driveclass;
Using abstract class objects to access numbering properties in abstract classes
Myclass.id = "BH0001";
Using abstract class objects to access name attributes in abstract classes
MyClass. Name = "TM";
To invoke an abstract method in an abstract class using an abstract class object
MyClass. Showinfo ();
}
}
}
In the example above, the abstract class is instantiated by the object Driveclass of the derived class, and then the abstract class is used to access the properties and methods in the abstract class.
C # Abstract classes and abstract methods