ホームページ >バックエンド開発 >C#.Net チュートリアル >Liskov 置換原則とは何ですか?また、それを C# で実装する方法は何ですか?
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 } }
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; } }
以上がLiskov 置換原則とは何ですか?また、それを C# で実装する方法は何ですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。