Home > Backend Development > C#.Net Tutorial > What is the Liskov Substitution Principle and how to implement it in C#?

What is the Liskov Substitution Principle and how to implement it in C#?

PHPz
Release: 2023-09-18 19:17:04
forward
764 people have browsed it

什么是里氏替换原则以及如何在 C# 中实现?

A derived type must be completely substitutable for its base type.

Definition:

We should be able to treat subclasses as parent classes. Essentially, this means that all derived classes should retain the functionality of their parent class and cannot replace any functionality provided by the parent class.

Before Liskov Replacement

public class Ellipse {
   public double MajorAxis { get; set; }
   public double MinorAxis { get; set; }

   public virtual void SetMajorAxis(double majorAxis){
      this.MajorAxis = majorAxis;
   }
   public virtual void SetMinorAxis(double minorAxis){
      this.MajorAxis = minorAxis;
   }
   public virtual double Area() {
      return MajorAxis * MinorAxis * Math.PI;
   }
}
public class Circle : Ellipse {
   public override void SetMajorAxis(double majorAxis) {
      base.SetMajorAxis(majorAxis);
      this.MinorAxis = majorAxis; //In a cirle, each axis is identical
   }
}

public class Result {
   public void Method1() {
      Circle circle = new Circle();
      circle.SetMajorAxis(5);
      circle.SetMinorAxis(4);
      var area = circle.Area(); //5*4 = 20, but we expected 5*5 = 25
   }
}
Copy after login

After Liskov Replacement

internal class Program {
   private static void Main() {
   }
}
public class Ellipse {
   public double MajorAxis { get; set; }
   public double MinorAxis { get; set; }

   public virtual void SetMajorAxis(double majorAxis) {
      MajorAxis = majorAxis;
   }
   public virtual void SetMinorAxis(double minorAxis) {
      MajorAxis = minorAxis;
   }
   public virtual double Area() {
      return MajorAxis * MinorAxis * Math.PI;
   }
}
public class Circle : Ellipse {
   public override void SetMajorAxis(double majorAxis) {
      base.SetMajorAxis(majorAxis);
      this.MinorAxis = majorAxis; //In a cirle, each axis is identical
   }
   public override void SetMinorAxis(double minorAxis) {
      base.SetMinorAxis(minorAxis);
      this.MajorAxis = minorAxis;
   }
   public override double Area() {
      return base.Area();
   }
}

public class Circle1 {
   public double Radius { get; set; }
   public void SetRadius(double radius) {
      this.Radius = radius;
   }
   public double Area() {
      return this.Radius * this.Radius * Math.PI;
   }
}
Copy after login

The above is the detailed content of What is the Liskov Substitution Principle and how to implement it in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template