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

chapter03 : interface 본문

JAVA/chapter18_interface

chapter03 : interface

GAWON 2023. 5. 30. 09:24
package org.joonzis.ex;
interface Eatable{
	void eat();	// public abstract void eat();
}
class Pig {
	public void piggy() {
		System.out.println("다 먹는다!");
	}
}
class Apple extends Pig implements Eatable{
	@Override
	public void eat() {
		System.out.println("사과는 맛있어");
	}
}
class Banana extends Pig implements Eatable{
	@Override
	public void eat() {
		System.out.println("맛있으면 바나나");
	}
}
class Computer extends Pig{
	
}
public class Ex03_Interface {
	public static void main(String[] args) {
		
		Pig[] pigking = new Pig[3];
		
		pigking[0] = new Apple();
		pigking[1] = new Banana();
		pigking[2] = new Computer();
		
		for(int i=0; i<pigking.length; i++) {
			pigking[i].piggy();
			// 사과는 맛있어
			// 맛있으면 바나나 
			// 출력
			// 출력하지 못하는 클래스는 "못 먹는다..." 출력
			
			if(pigking[i] instanceof Eatable) {
				((Eatable)pigking[i]).eat();
			}else {
				System.out.println("못 먹는다...");
			}
		}
	}
}

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

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