//1. 다형성이 적용되면 부모 = 자식
//2. 다형성에서 오버라이딩은 자식의 것.
class Shape {
public Shape() {
System.out.println("도형입니다.");
}
public void draw() {
System.out.println("도형을 그립니다.");
}
public double getArea() {
return 0.0;
}
}
class Rectangle extends Shape {
private double width,height;
public Rectangle() {
System.out.println("사각형 생성자");
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public void draw() {
System.out.println("사각형 도형을 그립니다.");
}
@Override
public double getArea() {
return width * height;
}
}
class Triangle extends Shape {
private double width,height;
public Triangle() {
System.out.println("삼각형 생성자");
}
public Triangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public void draw() {
System.out.println("삼각형 도형을 그립니다.");
}
@Override
public double getArea() {
return width * height / 2.0;
}
}
public class Poly {
public static void main(String[] args) {
Shape[] shape = {new Triangle(10,10),new Rectangle(10,10)};
double sumArea = 0;
for (Shape s : shape) {
sumArea += s.getArea();
}
System.out.println(sumArea);
}
}