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 : interface 본문

JAVA/chapter18_interface

chapter02 : interface

GAWON 2023. 5. 30. 09:23
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();
		
		
		
		
		
		
	}
}

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

Test . interface  (0) 2023.05.30
chapter03 : interface  (0) 2023.05.30
chapter01 : interface  (0) 2023.05.30