我有兩個類:Fullfill里氏替換原則
public class Base {
private final int value;
public Base(int value){
this.value = value;
}
public int getValue(){
return value;
}
public Base divide(Base that){
return new Base(getValue()/that.getValue());
}
}
public class Extended extends Base{
public Extended(int value){
super(value);
}
@Override public Extended divide(Base that){
return new Extended(getValue()/that.getValue());
}
public Extended getMax(Extended that){
return new Extended(Math.max(getValue(), that.getValue()));
}
}
Base b = new Base(3);
Extended c = new Extended(4);
我的目標是fullfill Liskov替換原則,但雙方類不fullfill這個原則呢。我認爲他們不會因爲這不起作用:
Extended d = c.getMax(b); //doesn't work
Extended e = c.getMax(c);
既能類fullfill的原則,如果我想的getMax
參數更改爲「基地那?
什麼是'b'?什麼是'c'? – Andremoniy
對不起,我忘了那些。 – binaryBigInt