JAVA/chapter18_interface
Test . interface
GAWON
2023. 5. 30. 09:26
Q1. Test01.java
스마트폰 => 전화기 + 컴퓨터
class Phone 필드 : String owner
메소드 : Constructor, sendCall(), receiveCall()
interface Computable 메소드 : connectInternet(), playApp()
class SmartPhone Phone 상속, Computable 구현
Q2. Test02.java
interface Providable 메소드: sightseeing(), leisure(), food()
class KoreaTour 메소드 : Providable 구현을 통해생성
class GuamTour 메소드 : Providable 구현을 통해생성
class TourGuide 필드 : Providable tour
메소드 : Constructor, sightseeing(), leisure(), food()
★ KoreaTour / GuamTour - Providable 구현
Q3. Test03.java
파일로 나눠서 실행해보자!!
Animal.java(인터페이스) 메소드 : move()
Dog.java(Animal 구현)
package org.joonzis.test;
public interface Animal {
public void move();
}
package org.joonzis.test;
public class Dog implements Animal{
@Override
public void move() {
System.out.println("강아지가 달린다.");
}
}
package org.joonzis.test;
class Phone{
private String owner;
public Phone(String owner) {
this.owner = owner;
}
public void sendCall() {
System.out.println(owner + "에게 전화 걸기");
}
public void receiveCall() {
System.out.println(owner + " 전화 받기");
}
}
interface Computable{
public void connectInternet();
public void playApp();
}
class SmartPhone extends Phone implements Computable{
public SmartPhone(String owner) {
super(owner);
}
@Override
public void connectInternet() {
System.out.println("인터넷 연결");
}
@Override
public void playApp() {
System.out.println("앱 실행");
}
}
public class Test01 {
public static void main(String[] args) {
//Phone phone = new SmartPhone("김씨");
//Computable phone = new SmartPhone("김씨");
SmartPhone phone = new SmartPhone("김씨");
phone.sendCall();
phone.receiveCall();
phone.connectInternet();
phone.playApp();
}
}
package org.joonzis.test;
interface Providable{
public void sightseeing();
public void leisure();
public void food();
}
class KoreaTour implements Providable{
@Override
public void sightseeing() {
System.out.println("경복궁");
}
@Override
public void leisure() {
System.out.println("디스코 팡팡");
}
@Override
public void food() {
System.out.println("김치찌개");
}
}
class GuamTour implements Providable{
@Override
public void sightseeing() {
System.out.println("사랑의 절벽");
}
@Override
public void leisure() {
System.out.println("난파선 다이빙");
}
@Override
public void food() {
System.out.println("치킨 칼라구엔");
}
}
class TourGuide{
private Providable tour;
public TourGuide() {}
public TourGuide(Providable tour) {
this.tour = tour;
}
public void sightseeing() {
tour.sightseeing();
}
public void leisure() {
tour.leisure();
}
public void food() {
tour.food();
}
}
public class Test02 {
public static void main(String[] args) {
TourGuide tour1 = new TourGuide(new KoreaTour());
TourGuide tour2 = new TourGuide(new GuamTour());
tour1.sightseeing();
tour1.leisure();
tour1.food();
System.out.println("---------------");
tour2.sightseeing();
tour2.leisure();
tour2.food();
}
}
package org.joonzis.test;
public class Test03 {
public static void main(String[] args) {
Animal dog = new Dog();
dog.move();
}
}