package org.joonzis.ex;
interface Shape{
double PI = Math.PI;
double calcArea();
void output();
}
class Rect implements Shape{
private int width;
private int height;
public Rect() {}
public Rect(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public double calcArea() {
return width * height;
}
@Override
public void output() {
System.out.println("너비 : " + width);
System.out.println("높이 : " + height);
System.out.println("크기 : " + calcArea());
}
}
class Circle implements Shape{
private double radius;
public Circle() {}
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calcArea() {
return PI * Math.pow(radius, 2);
}
@Override
public void output() {
System.out.println("반지름 : " + radius);
System.out.println("넓이 : " + calcArea());
}
}
public class Ex02_Interface {
public static void main(String[] args) {
// 도형 배열을 생성하여 사각형, 원형 객체를 저장.
// 각 도형의 정보 출력
// 1. 사각형 : 너비, 높이, 크기(너비*높이)
// 2. 원형 : 반지름, 넓이(PI*radius*radius)
Shape[] shapes = new Shape[2];
shapes[0] = new Rect(2, 3);
shapes[1] = new Circle(1.5);
shapes[0].output();
System.out.println("-----------------");
shapes[1].output();
}
}