Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter02 : polymorphism 본문

JAVA/chapter16_polymorphism

chapter02 : polymorphism

GAWON 2023. 5. 26. 18:51
package org.joonzis.ex;
class Shape{
	// 의미 없는 메소드(자식들이 사용하기 위해 만들어놓음)
	public double calcArea() {
		return 0;
	}
}
class Rect extends Shape{
	private int width, height;
	public Rect(int width, int height) {
		this.width = width;
		this.height = height;
	}
	@Override
	public double calcArea() {
		return width * height;
	}
}
class Triangle extends Shape{
	private int width, height;
	public Triangle(int width, int height) {
		this.width = width;
		this.height = height;
	}
	@Override
	public double calcArea() {
		return (width * height) / 2.0;
	}
}
class Circle extends Shape{
	private double radius;
	public Circle(double radius) {
		this.radius = radius;
	}
	@Override
	public double calcArea() {
		return Math.PI * Math.pow(radius, 2);
	}
}
public class Ex02_polymorphism {
	public static void main(String[] args) {
		
		// 길이 3 짜리 부모 배열을 선언하여
		// 각각 인덱스에 사각형, 삼각형, 원형 클래스 객체 대입
		// 각 객체별 크기 출력
		Shape[] arr = new Shape[3];
		
		arr[0] = new Rect(2, 3);
		arr[1] = new Triangle(2, 3);
		arr[2] = new Circle(3.2);
		
		System.out.println(arr[0].calcArea());		
		System.out.println(arr[1].calcArea());		
		System.out.println(arr[2].calcArea());		
		
		for(int i=0; i<arr.length; i++) {
			System.out.println("크기 : " + arr[i].calcArea());
		}
				
		
		
	}
}

'JAVA > chapter16_polymorphism' 카테고리의 다른 글

Test . polymorphism  (0) 2023.05.30
chapter04 : polymorphism  (0) 2023.05.26
chapter03 : polymorphism  (0) 2023.05.26
chapter01 : polymorphism  (0) 2023.05.26