[C# Study Notes][Abstract]

C# study notes series

Chapter 1 [C# Study Notes] [StackTrace]
Chapter 2 [C# Study Notes] [Attribute]
Chapter 3 [C# Study Notes] [Interface]
Chapter 4 [C# Study Notes] [GUID]
Chapter 5 [C# Study Notes] [Virtual]
Chapter 6 [C# Study Notes] [Abstract]



Preface

Detailed explanation of abstract classes in C#.


1. Introduction to abstract

  • The abstract modifier can be used on classes, methods, properties, indexes, and events.
  • The abstract modifier is used to simply summarize an incomplete content (abstract). Often used in base classes, the derived class inherits the abstract properties and methods that implement the base class.
  • An abstract class is an abstraction with the same behavior and characteristics. Members in an abstract class do not specify specific details, and the specific content is generally implemented in its derived class.
  • Add the abstract modifier before a normal class to mark it as an abstract class. If an ordinary class uses the abstract modifier on its members, the class must be modified as an abstract class.
  • Abstract classes cannot be modified using the sealed modifier because the two modifiers have opposite meanings. The sealed modifier prevents the class from being inherited, while the abstract modifier requires the class to be inherited.
  • Derived classes that inherit abstract methods must implement all members in the override abstract class and use override rewriting to achieve this.
  • Abstract classes cannot be instantiated, but derived classes can be instantiated.
  • Abstract methods must be declared in abstract classes. When declaring abstract methods, virtual, static, and private modifiers cannot be used.
  • Abstract method declarations do not provide an actual declaration and therefore have no method body, similar to interfaces but unlike virtual methods.

2. Use of abstract classes

2.1.Declaration form

abstract class Person
{
    
    
    public abstract void IDentity();
}

2.2. Calling methods

abstract class Person
{
    
    
    public abstract void IDentity();
}
class Boy:Person
{
    
    
    public override void IDentity()
    {
    
    
        Console.WriteLine("我是一个人");
    }
}

3. Application scenarios of abstract classes

A simple description of an abstract class is: I am Lao Tzu (abstract class). If you follow (inherit) Lao Tzu, you must know what to do (actual implementation). For example, if I know how to be an adult, then you must also know how to be an adult
. , but you decide whether to hit gently or hard, but you must be able to hit people.

If a class is designed to be inherited by other classes and represents the public properties or methods of a class of objects, then the class should be set as an abstract class.

4. Compare Interface, Virtual, Abstract

It is easy to confuse these three because they are all related to inheritance and involve the use of override.

Virtual methods have specific implementations, while methods in the interface interface and abstract abstract methods do not have specific implementations;
virtual methods can be overridden or not overridden in derived classes, while methods in the interface interface and abstract abstract methods must Explicitly overridden;
The interface interface can be multiple-inherited, and the class where virtual is located and the abstract abstract class belong to the base class and cannot be multiple-inherited;
interface is just a behavioral specification, and the abstract abstract class is an incomplete class, which is object abstraction

Summarize

How can you reach a thousand miles without accumulating steps?

Supongo que te gusta

Origin blog.csdn.net/Aflashstar/article/details/128730146
Recomendado
Clasificación