peaches-book-study / effective-java

이펙티브 자바 3/E
0 stars 2 forks source link

Item 23. 태그 달린 클래스보다는 클래스 계층구조를 활용하라 #24

Open byunghyunkim0 opened 6 months ago

byunghyunkim0 commented 6 months ago

Chapter : 4. 클래스와 인터페이스

Item : 23. 태그 달린 클래스보다는 클래스 계층구조를 활용하라

Assignee : byunghyunkim0


🍑 서론

class Circle extends Figure { final double radius;

Circle(double radius) {
    this.radius = radius;
}

@Override
double area() {
    return Math.PI * (radius * radius);
}

}

class Rectangle extends Figure { final double length; final double width;

Rectangle(double length, double width) {
    this.length = length;
    this.width = width;
}

@Override
double area() {
    return length * width;
}

}

### 정사각형도 지원하도록 수정하려면?
```java
class Square extends Rectangle {
    Square(double side) {
        super(side, side);
    }
}

Referenced by