liskov-substitution-principle

Can you explain Liskov Substitution Principle with a good C# example?

两盒软妹~` 提交于 2019-11-26 23:35:02
Can you explain Liskov Substitution Principle (The 'L' of SOLID) with a good C# example covering all aspects of the principle in a simplified way? If it is really possible. jgauffin (This answer has been rewritten 2013-05-13, read the discussion in the bottom of the comments) LSP is about following the contract of the base class. You can for instance not throw new exceptions in the sub classes as the one using the base class would not expect that. Same goes for if the base class throws ArgumentNullException if an argument is missing and the sub class allows the argument to be null, also a LSP

Why it's impossible to override `var` with `def` in Scala?

廉价感情. 提交于 2019-11-26 22:04:40
问题 While I understand why a var cannot override a val in subclass and vice versa, I am unable to understand why does Scala not allow a def in subclass to override a var in superclass class Car { var age = 32 } class SedanCar extends Car { override def age = 54 } As var is mutable why not allow a def to override it? Can anyone please help me in understanding this? 回答1: That's related to the Liskov Substitution Principle: you can't assign weaker access privileges in subclass (even for Java).

Can anyone provide an example of the Liskov Substitution Principle (LSP) using Vehicles?

我只是一个虾纸丫 提交于 2019-11-26 18:56:59
问题 The Liskov Substitution Principle states that a subtype should be substitutable for that type (without altering the correctness of the program). Can someone please provide an example of this principle in the domain of vehicles (automotives)? Can someone please provide an example of a violation of this principle in the domain of vehicles? I've read about the square/rectangle example, but I think that an example with vehicles will give me a better understanding of the concept. 回答1: For me, this

Why array implements IList?

只愿长相守 提交于 2019-11-26 10:28:42
See the definition of System.Array class public abstract class Array : IList, ... Theoretically, I should be able to write this bit and be happy int[] list = new int[] {}; IList iList = (IList)list; I also should be able to call any method from the iList ilist.Add(1); //exception here My question is not why I get an exception, but rather why Array implements IList ? Because an array allows fast access by index, and IList / IList<T> is are the only collection interfaces that support this. So perhaps your real question is "Why is there no interface for constant collections with indexers?" And to

Can you explain Liskov Substitution Principle with a good C# example? [closed]

蹲街弑〆低调 提交于 2019-11-26 08:44:37
问题 Can you explain Liskov Substitution Principle (The \'L\' of SOLID) with a good C# example covering all aspects of the principle in a simplified way? If it is really possible. 回答1: (This answer has been rewritten 2013-05-13, read the discussion in the bottom of the comments) LSP is about following the contract of the base class. You can for instance not throw new exceptions in the sub classes as the one using the base class would not expect that. Same goes for if the base class throws

Is deriving square from rectangle a violation of Liskov&#39;s Substitution Principle? [closed]

大憨熊 提交于 2019-11-26 04:20:28
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 12 months ago . I am new to design and learning the design principles. It says deriving square from rectangle is a classic example of violation of Liskov\'s Substitution Principle. If that\'s the case, what should be the correct design? 回答1: I believe the reasoning is something like this:

Why array implements IList?

[亡魂溺海] 提交于 2019-11-26 02:09:33
问题 See the definition of System.Array class public abstract class Array : IList, ... Theoretically, I should be able to write this bit and be happy int[] list = new int[] {}; IList iList = (IList)list; I also should be able to call any method from the iList ilist.Add(1); //exception here My question is not why I get an exception, but rather why Array implements IList ? 回答1: Because an array allows fast access by index, and IList / IList<T> is are the only collection interfaces that support this.

Why doesn&#39;t Rust support trait object upcasting?

有些话、适合烂在心里 提交于 2019-11-26 00:24:12
问题 Given this code: trait Base { fn a(&self); fn b(&self); fn c(&self); fn d(&self); } trait Derived : Base { fn e(&self); fn f(&self); fn g(&self); } struct S; impl Derived for S { fn e(&self) {} fn f(&self) {} fn g(&self) {} } impl Base for S { fn a(&self) {} fn b(&self) {} fn c(&self) {} fn d(&self) {} } Unfortunately, I cannot cast &Derived to &Base : fn example(v: &Derived) { v as &Base; } error[E0605]: non-primitive cast: `&Derived` as `&Base` --> src/main.rs:30:5 | 30 | v as &Base; | ^^^^