C beginner's course series 12: abstract classes
gentleman 2020-11-27 09:14:43

This article is about C# Simple tutorial for beginners , This is the first 12 piece . Thank you for watching. , Remember to follow me , There are more tutorial articles to follow , thank you .

The environment of this paper is Visual Studio 2019.

One 、 What is an abstract class

Abstract class is an abstract representation of every object in the real world , Usually, only the characteristics it has are defined 、 function 、 Relationship , But don't do concrete implementation , That is, abstract class . This explanation is exclusive to , All over the Internet .

Two 、 Code definition of abstract classes

stay C# in , By using abstract Keyword to define a class . Because abstract classes are abstract definitions of real-world objects , No specific implementation , So abstract classes are designed to prohibit instantiation ( That is to prohibit new An object ).

3、 ... and 、 Some requirements for abstract classes

For example, when you define an abstract class , May need to use abstract Keyword to define some methods or properties , Or use virtual Virtualize the method . But it's not necessary , But you want to define an abstract class , That's what it was meant to be .

Four 、 Characteristics of abstract classes

1. No instantiation .

2. There should be abstract or virtual Method , Although this is not necessary , But that's what you wanted to do .

3. No use sealed keyword , because abstract and sealed On the contrary , Is contradictory .

4. All subclasses that inherit from abstract classes , All must be realized abstract Method or property , Can be rewritten virtual Method .

5. Abstract method (abstract) Can only be used in abstract classes .

6. Abstract methods themselves are implicit virtual methods (virtual), therefore , You can't add... To the same method at the same time abstract and virtual keyword .

5、 ... and 、 Implementing abstract classes

Use a colon (:) Let a class inherit or implement an abstract class .

6、 ... and 、 For example

We often eat fish , Please look at the chart below. :

C# Beginner's course series 12: abstract class

C# Beginner's course series 12: abstract class

analysis :

1. Weigh , It's an abstract property .

2. Killing fish , There are different ways to kill fish , Some may have to be boned , Such as sauerkraut fish .

3. cooking , There should be a recipe , Different fish may do different things .

4. Tilapia mossambica , Use a colon , Implemented abstract base class . Then tilapia implements its attribute method , For example, when tilapia is bought 1 Jin 3 two , The way to kill fish is not to remove bones , The cooking method is to fry . If it's grass carp , Then we need to get rid of the bone , And then cook it .

Have a good meal .