package com.haenin.section03.liskovsustitution.resolved;
/* 설명.
* Rectangle과 Square는 다른 부분이 많으니 별도의 타입으로 정의하고
* 공통적인 부분만 인터페이스를 통해 필요한 동작만 추상화하여 LSP를 준수한다.*/
public class Application {
public static void main(String[] args) {
Shape rectangle = new Rectangle(5,10);
System.out.println("직사각형의 넓이: " + rectangle.getArea());
Shape square = new Square(5);
System.out.println("정사각형의 넓이: " + square.getArea());
}
}
package com.haenin.section03.liskovsustitution.resolved;
public interface Shape {
int getArea();
}
package com.haenin.section03.liskovsustitution.resolved;
public class Rectangle implements Shape{
private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public int getArea() {
return width * height;
}
}
package com.haenin.section03.liskovsustitution.resolved;
public class Square implements Shape{
private int side;
public Square(int side) {
this.side = side;
}
@Override
public int getArea() {
return (int)Math.pow(side,2);
}
}