본문 바로가기

CS Study/Java

8. 인터페이스와 추상 메소드

1. 추상메소드만 담고 있는 인터페이스(property)를 클래스가 implement

import java.util.Scanner;


class dog implements property {
	public void sound() {
		System.out.println("소리 : 멍멍!");
	}
	public void color() {
		System.out.println("색상 : Gold");
	}
	public void specialty() {
		System.out.println("특기 : 으르렁대며 물기");
	}

}

class cat implements property {
	public void sound() {
		System.out.println("소리 : 야옹");
	}
	public void color() {
		System.out.println("색상 : Gray");
	}
	public void specialty() {
		System.out.println("특기 : 우아한 척하며 털 고르기");
	}
}

class chicken implements property {
	public void sound() {
		System.out.println("소리 : 꼬끼오");
	}
	public void color() {
		System.out.println("색상 : Red");
	}
	public void specialty() {
		System.out.println("특기 : 날아오르며 부리로 찍기");
	}
	

}

interface property {
	void sound();
	void color();
	void specialty();	

}

public class main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.print("번호 선택(1~3) : ");
		Scanner scan = new Scanner(System.in);
		int num = scan.nextInt();
		dog dog1 = new dog();
		cat cat1= new cat();
		chicken chicken1 = new chicken();
		
		while (0< num && num < 4) {
			switch(num) {
			case 1:
				dog1.sound();
				dog1.color();
				dog1.specialty();
				break;
			case 2: 
				cat1.sound();
				cat1.color();
				cat1.specialty();
				break;
			case 3:
				chicken1.sound();
				chicken1.color();
				chicken1.specialty();
				break;
			default : 
				
				break;
			}
			System.out.print("번호 선택(1~3) : ");
			num = scan.nextInt();
		}
		System.out.println("종료합니다.");
		
	}

}

2. 통장 이름과 잔고 출력 - 객체 활용

public class Account {
	private String name;
	private int balance;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getBalance() {
		return balance;
	}
	public void setBalance(int balance) {
		this.balance = balance;
	}

}


public class AccountTEst {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Account obj = new Account ();
		obj.setName("Tom");
		obj.setBalance(100000);
		System.out.println("이름 = "+obj.getName());
		System.out.println("통장 잔고 = "+ obj.getBalance());

	}

}

3. Student가 person을 상속 - Student가 person의 메소드를 오버라이딩

class Person{
	private int weight;
	int age;
	protected int height;
	public String name;
	
	public void setWeight(int weight) {
		this.weight = weight;
	}
	public int getWeight() {
		return this.weight;
	}
}
class Student extends Person {
	public void set() {
		age = 30;
		name = "홍길동";
		height = 175;
		setWeight(99);
	}
}
public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person p = new Person();
		System.out.print(p.getWeight());
		System.out.println(","+p.age+","+p.height+","+p.name);
		Student s = new Student();
		s.set();
		System.out.print(s.getWeight());
		System.out.println(","+s.age+","+s.height+","+s.name);
	}

}

4. 텔레비전 기능 구현하기 - 인터페이스 활용(Television객체가 RemoteControl를 인터페이스로 삼음)

public interface RemoteControl {
	public abstract void turnOn();
	public abstract void turnOff();

}

public class Television implements RemoteControl {
	boolean onOff = false;
	public void turnOn() {
		onOff = true;
	}
	public void turnOff() {
		onOff = false;
	}

}

public class TelevisionTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Television t = new Television();
		t.turnOn();
		System.out.println("전원ON : " + t.onOff);
		t.turnOff();
		System.out.println("전원ON : "+t.onOff);

	}

}

5. 휴대폰 기능 구현하기  - 인터페이스 활용 (AIPhone이 PhoneInteface, AIInterface를 인터페이스로 삼음)

public interface PhoneInterface {
	public static final int TIMEOUT = 10000;
	void sendCall();
	void receiveCall();
	default void printLogo() {
		System.out.println("** Phone **");
	}
	

}

interface AIInterface {
	void recognizeSpeech();
	void synthesizeSpeech();

}

class AIPhone implements PhoneInterface, AIInterface {

	public void sendCall() {
		System.out.println("띠리리리링"); 
	}

	public void receiveCall() {
		System.out.println("전화가 왔습니다."); 
	}
	public void recognizeSpeech() {
		System.out.println("음성인식 중 입니다.");
	}
	public void synthesizeSpeech() {
		System.out.println("음성 합성 중 입니다.");
	}

	public void flash() {
		System.out.println("불이 켜졌습니다."); 
	} 
}
public class Test1 {

	public static void main(String[] args) {
		AIPhone phone;
		phone = new AIPhone();
		phone.printLogo();
		phone.sendCall();
		phone.receiveCall();
		phone.recognizeSpeech();
		phone.synthesizeSpeech();
		phone.flash();
		
		
		// TODO Auto-generated method stub

	}

}

'CS Study > Java' 카테고리의 다른 글

11. 컬렉션  (0) 2022.04.07
10. 제네릭  (0) 2022.04.07
7. 상속 - 메소드 오버라이딩  (0) 2022.04.07
6. 패키지 / 접근 제한자 / 상속  (0) 2022.04.07
5. 클래스 / 객체 활용  (0) 2022.04.07