C # can still apply the Liskov substitution principle.
Consider:
public class Base1
{
}
public class Derived1 : Base1
{
}
public class Base2
{
public virtual Base1 Method()
{
return new Base1();
}
}
public class Derived2 : Base2
{
public override Base1 Method()
{
return new Derived1();
}
}
If C # supports covariant return types, then an override for Method()in Base2can be declared as follows:
public class Derived2 : Base2
{
public override Derived1 Method()
{
return new Derived1();
}
}
# , , , Base1.
.
:
Base2 test = new Base2();
Base1 item = test.Method();
:
Base2 test = new Derived2();
Base1 item = test.Method();
new Base2() new Derived2() . .