官术网_书友最值得收藏!

  • C# 7 and .NET Core Cookbook
  • Dirk Strauss
  • 230字
  • 2021-07-03 00:11:57

Creating and implementing an abstract class

Many developers have heard about abstract classes, but their implementation is a mystery. How can you as a developer identify an abstract class and decide when to use one? The definition is quite a simple one actually. Once you understand this fundamental definition of an abstract class, when and why to use one becomes obvious.

Imagine for a moment that you are developing an application that manages the animals in a cat sanctuary. The cat sanctuary rehabilitates lions, tigers, jaguars, leopards, cheetahs, pumas, and even domestic cats. The common noun that describes all these animals is the word cat. You can, therefore, safely assume that the abstraction of all these animals is a cat, and thus, this word identifies our abstract class. You would then create an abstract class called Cat.

However, you need to keep in mind that you will never ever create an instance of the abstract class Cat. All the classes that inherit from the abstract class also share some functionality. This means that you will create a Lion class and a Tiger class that inherit from the abstract class Cat. In other words, the inherited classes are a kind of cat. Both classes share functionality in the form of Sleep(), Eat(), Hunt(), and various other methods. In this way, we can ensure that inherited classes all contain this common functionality.

主站蜘蛛池模板: 理塘县| 周口市| 石家庄市| 黔南| 宜城市| 交口县| 保山市| 厦门市| 阿克苏市| 深圳市| 瓦房店市| 长寿区| 探索| 资兴市| 涞水县| 清原| 辉县市| 荃湾区| 海宁市| 屏山县| 安平县| 淮安市| 宣恩县| 台湾省| 神木县| 廊坊市| 昌江| 修文县| 彭山县| 鹤山市| 临西县| 云林县| 扎兰屯市| 大港区| 体育| 阿坝县| 浦城县| 根河市| 收藏| 会理县| 登封市|