Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the principle of C # Richter scale replacement

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to realize the principle of C# Richter replacement". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to realize the principle of replacement of C# Richter scale" can help you solve your doubts. Let's follow the editor's train of thought to learn new knowledge.

Richter substitution principle (LSP)

Definition: wherever the parent class appears, it can be replaced with its subclass without affecting the functionality.

Explanation:

In fact, LSP is an extension of the opening and closing principle. In OO's thought, we know that an object is composed of a series of states and behaviors. The Richter substitution principle says that in an inheritance system, objects should have common external characteristics. When using LSP, if you want our program to reach the place where a parent class appears, it can be replaced with its subclasses without affecting the function. Then this parent class should also try to declare some common methods needed by the subclass. after the parent class is replaced by the subclass, it will be relatively smooth, so why is it an extension of the open and closed principle? Because we say in the open-close principle to use interfaces and abstract classes as much as possible, of course, this abstract class and interface should also be fully defined as far as possible, so that our interface and abstract class will be more stable. this accords with both the opening and closing principle and the Richter substitution principle.

Error case 1:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks Namespace Richter replacement principle {/ public class Bird {/ the method of eating / public void Eat () {} / public void Fly () }} / define a penguin inherited bird / public class Penguin: Bird {} public class Test {public static void ShowFly (Bird bird) {bird.Fly () } public static void Main () {ShowFly (new Penguin ());}

Explanation:

In the above code, a bird is defined, and penguins inherit from birds. Birds have a way to fly, and penguins can't fly, so the above code violates the Richter scale replacement principle.

Error case 2:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks Namespace Richter substitution principle {/ define a parent pet class / public class Pet {} / define a penguin class inherited from the pet class / public class PenguinDemo: Pet {/ swimming method / public void Swiming () {Console.WriteLine ("cute penguins are swimming") } / define a dolphin class inherited from the pet class / public class Dolphin: Pet {/ public void PlayGame () {Console.WriteLine ("Magic Bubble Brick") }} / Test class / public class Test {public static void ShowPlay (Pet pet) {if (pet is PenguinDemo) {/ / Type conversion PenguinDemo pen = (PenguinDemo) pet; pen.Swiming () } if (pet is Dolphin) {/ / Type conversion Dolphin dol = (Dolphin) pet; dol.PlayGame ();}

Explanation:

If we take puffer fish and penguins as pets, we can define a pet class, and then let these pets inherit this category. We know that we play differently with each kind of pet. such as. Penguin has swimming method, puffer fish has game method, according to this requirement, we design a system, write a pet class, let penguin inherit this pet class, create a swimming method in penguin class, this method can not be put into pet class, because not all pets can swim. When writing the puffer fish class, he is also asked to inherit the pet class and write a game method in the puffer fish class. At this time, when the client program uses the pet class and its subclass, it needs to judge which subclass it is. We cannot call the specific method through the pet class. To make a judgment and transformation, if we add a dog class, the dog class will also have an independent method. To modify the previous code (when using the pet class and its subclass, add the judgment to be a dog), this is obviously not in line with the open and closed principle, and it is impossible to comply with the Richter replacement principle, because none of the pets can replace his parent class, because their behavior is different, the maintainability and reusability of the code is very poor!

After reading this, the article "how to realize the principle of replacement on the Richter scale" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report