-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLiskovSubstitution_counter.java
71 lines (59 loc) · 1.84 KB
/
LiskovSubstitution_counter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package src;
/**
* 这个例子违反了里氏代换原则,为了方面理解,把全部类放在了一起。
* 1. 子类覆盖了父类的方法,改变了父类方法的意图。
* 2. 因为子类改变了父类行为,如果用子类替换其父类可能会引起问题。
*/
public class LiskovSubstitution_counter {
public LiskovSubstitution_counter() {
return;
}
// 图形父类
public abstract class Shape {
public void draw() {
System.out.println("Drawing Shape. area:" + this.area());
}
public abstract double area();
}
// 正方形子类,继承自图形类
public class Square extends Shape {
private double side;
public Square(double side) {
this.side = side;
}
// 重写父类的draw方法,同时改变了父类的行为
@Override
public void draw() {
if (checkArea()) {
System.out.println("Drawing Square. area:" + this.area());
} else {
// 这里存在子类其他的行为,与父类预期不一致
System.out.println("Don't draw square");
}
}
public boolean checkArea() {
if (this.area() > 100) {
return false;
}
return true;
}
@Override
public double area() {
return side * side;
}
}
// 矩形类,继承自图形类
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// 这里没有覆盖父类的draw()方法
@Override
public double area() {
return width * height;
}
}
}