728x90

유투브 강좌를 보고 따라서 코드 적어가며 테스트하고 적어둔다.

유투브 동영상 : https://www.youtube.com/watch?v=OTNuxe_ihtM&t=774s

 

 

각각 서로 다른 파일로 생성하여 테스트 해야 하는데 하나의 파일로 작성하여 테스트했다.

- 인터페이스 타입의 변수로 인터페이스를 구현한 클래스의 인스턴스를 참조할 수 있다.

- 인터페이스를 메소드의 매개변수 타입으로 지정할 수 있다.

interface OnFoundListener {
    // 1. Create a callback interface.
    void onFound(String result);
}
 
class ClassA {
    // 2. Create a class as a worker.
    public void findAGirl(OnFoundListener onFoundListener ) {
        for(int i=0; i < 5; i++) {
            try {
                Thread.sleep(100);
                System.out.println("A is trying find a girl for you " + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        onFoundListener.onFound("A found a girl for you.");
        // A found nothing, so that assign this job for B.
        //ClassB classB = new ClassB();
        //classB.findACuteGirl(onFoundListener);
    }
 
}
 
public class InerfaceWorld {
    public static void main(String[] args) {
        ClassA classA = new ClassA();
        classA.findAGirl(new OnFoundListener() {
            @Override
            public void onFound(String result) {
                System.out.println(result);
            }
        });
    }
}
 

메소드를 호출하는 쪽에서는 메소드의 내용에 관계없이 선언부만 알면 되기 때문에 이를 이용하여 프로그램을 작성할 수 있다. 동시에 다른 한 쪽에서는 인터페이스를 구현하는 클래스를 작성하도록 하여, 인터페이스를 구현하는 클래스가 작성될 때까지 기다리지 않고도 양쪽에서 동시에 개발을 진행할 수 있다.

 

 

 

 

 

interface OnFoundListener {
    // 1. Create a callback interface.
    void onFound(String result);
}
 
class ClassA {
    // 2. Create a class as a worker.
    public void findAGirl(OnFoundListener onFoundListener ) {
        for(int i=0; i < 5; i++) {
            try {
                Thread.sleep(100);
                System.out.println("A is trying find a girl for you " + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //onFoundListener.onFound("A found a girl for you.");
        // A found nothing, so that assign this job for B.
        ClassB classB = new ClassB();
        classB.findACuteGirl(onFoundListener);
    }
}
 
class ClassB {
    public void findACuteGirl(OnFoundListener onFoundListener ) {
        for(int i=0; i < 5; i++) {
            try {
                Thread.sleep(100);
                //System.out.println("A is trying find a girl for you " + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        onFoundListener.onFound("A found a cute girl for you.");
    }
}
 
public class InterfaceWorld2 {
    public static void main(String[] args) {
        ClassA classA = new ClassA();
        classA.findAGirl(new OnFoundListener() {
            @Override
            public void onFound(String result) {
                System.out.println(result);
            }
        });
    }
}
 
 

 

 

'안드로이드 > Interface' 카테고리의 다른 글

Java Interface 예제  (0) 2019.11.18
Android Interface AsyncTask 예제  (0) 2019.11.05
Java 인터페이스(interface) 개요  (0) 2019.08.20
Android Interface 예제 ★★★  (0) 2018.08.22
Android Interface 예제 1  (0) 2018.08.20
블로그 이미지

Link2Me

,