C beginner's tutorial series 11: Inheritance
Old horse and pony 2020-11-27 05:12:53

This article is about C# Simple tutorial for beginners , This is the first 11 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 inheritance

Inheritance is a basic feature of object-oriented programming . By means of inheritance , Be able to define reusability ( Inherit )、 A subclass that extends or modifies the behavior of a parent class . A class whose members are inherited is called a base class . A class that inherits members of a base class is called a derived class .

such as , The breast length Ru skirt is a kind of Han clothing , A model family is a kind of family , Tilapia 、 Perch is a kind of fish .

that : The shape of Hanfu 、 family 、 fish , It's the base class , It's also called the parent . Ruskirt with chest together 、 Model family 、 Tilapia and perch , Is a derived class , Also called subclass .

Two 、C# The way to write inheritance in

C# Beginner's course series 11: Inherit


C# Beginner's course series 11: Inherit


Pictured above , The inheritance syntax is to use an English half colon (:) To define .

3、 ... and 、C# The main point of inheritance in

1、 stay C# in , Inherit , It's single inheritance , It means every subclass , There can be at most one parent class .

2、 stay C# in , Inheritance is hierarchical , It can be inherited from generation to generation , It's kind of like the protoss genealogy .

C# Beginner's course series 11: Inherit


C# Beginner's course series 11: Inherit


?

Pictured above , The father of perch is fish , The father of fish is animal , The father of an animal is a creature , There are layers of inheritance .

3、 stay C# in , All classes , They are all implicitly inherited from Object Base class . Because of that , therefore , What you see above “ fish ” class , It's empty inside, nothing , But if you output , You'll find something else , These things are actually made of Object Base class . As shown in the figure below :

C# Beginner's course series 11: Inherit


C# Beginner's course series 11: Inherit


?

Red circle of definition , It's an implicit base class Object Defined .

Four 、 abstract class

Once you inherit a class , Then this class is called the base class ( Parent class ) 了 .

If you expect the base class not to be instantiated ( namely new An object ), Then you can add abstract keyword .

5、 ... and 、 Sealing class

If you expect a class , It's not allowed to be inherited , Then you can add sealed keyword , It means sealing .

C# Beginner's course series 11: Inherit


C# Beginner's course series 11: Inherit


?

Pictured above , Once in sealed keyword , Derivation will be prohibited .

Have a good meal .
版权声明
本文为[Old horse and pony]所创,转载请带上原文链接,感谢
https://fheadline.com/2020/11/202011270510343625.html
相似文章

2021-08-09