nme.kr

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
프로그램:java:ㄴhomework:book_source:ch09 [2022/01/04 10:42]
clayeryan@gmail.com
프로그램:java:ㄴhomework:book_source:ch09 [2023/07/13 17:29] (현재)
clayeryan@gmail.com ↷ 문서가 programmer:java:ㄴhomework:book_source:ch09에서 프로그램:java:ㄴhomework:book_source:ch09(으)로 이동되었습니다
줄 1: 줄 1:
-====== Chaptor09 인터페이스 ======+====== Ch.09 인터페이스 ====== 
 + 
 +===== 인터페이스 선언 ===== 
 + 
 +**인터페이스의 구조** 
 + 
 +  [public] interface 인터페이스명 { 
 +       [public abstract] 리턴타입 추상메서드명(); 
 +       [public] default 리턴타입 메서드명 (매개변수...) {...} 
 +       [public] static 리턴타입 메서드명(매개변수...) {...} 
 +  } 
 +   
 + 
 +:wr:<color #ff7f27>**상수**</color> 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface InterfaceEx { 
 + 
 + // 상수 
 + public int MIN_PRICE = 0; 
 + public int MAX_PRICE = 100000; 
 +  
 + // 추상 메서드 
 + public double meanPrice(); 
 + public double totalPrice(); 
 +  
 + // default 메서드 
 + default double getSalePrice(double price) { 
 + return price - (price * 0.05); 
 +
 +  
 + // static 메서드 
 + static void printPrice(double price) { 
 + System.out.println(price); 
 +
 +  
 +
 + 
 +</code> 
 + 
 +===== 인터페이스 구현 ===== 
 + 
 +**Printer.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface Printer { 
 + 
 + int INK = 100; 
 + void print(); 
 +  
 +
 +</code> 
 + 
 +**Scanner.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface Scanner { 
 + 
 + void scan(); 
 +  
 +
 + 
 +</code> 
 + 
 +**Fax.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface Fax { 
 + 
 + String FAX_NUMBER = "02-1234-5678"; 
 + void send(String tel); 
 + void receive(String tel); 
 +  
 +
 + 
 +</code> 
 + 
 +**Complexer.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class Complexer implements Printer, Scanner, Fax { 
 + 
 + @Override 
 + public void send(String tel) { 
 + System.out.println(FAX_NUMBER + "에서 "+tel+"로 FAX 전송"); 
 +  
 +
 + 
 + @Override 
 + public void receive(String tel) { 
 + System.out.println(tel + "에서 "+FAX_NUMBER+"로 FAX 수신"); 
 +  
 +
 + 
 + @Override 
 + public void scan() { 
 + System.out.println("스캔 실행"); 
 +  
 +
 + 
 + @Override 
 + public void print() { 
 + System.out.println("출력 실행"); 
 +  
 +
 + 
 +  
 +
 + 
 +</code> 
 + 
 +**ComplexerMain.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class ComplexerMain { 
 + 
 + public static void main(String[] args) { 
 +  
 + Complexer com = new Complexer(); 
 + System.out.println(Complexer.INK); 
 + System.out.println(Complexer.FAX_NUMBER); 
 + com.print(); 
 + com.scan(); 
 + com.send("02-8765-4321"); 
 + com.receive("02-8765-4321"); 
 + 
 +
 + 
 +
 + 
 +</code> 
 + 
 +:wr:<color #ff7f27>**익명구현객체**</color> 
 + 
 +  인터페이스 객체명 = new 인터페이스(){ 
 +  // 인터페이스의 모든 추상 메서드 구현 
 +  }; 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class ComplexerMain2 { 
 + 
 + public static void main(String[] args) { 
 +  
 + Fax fax = new Fax() { 
 + 
 + @Override 
 + public void send(String tel) { 
 + System.out.println("여기는 익명 구현 객체의 send()"); 
 +  
 +
 + 
 + @Override 
 + public void receive(String tel) { 
 + System.out.println("여기는 익명 구현 객체의 receive()"); 
 +  
 +
 +  
 + }; 
 +  
 + fax.send("1234"); 
 + fax.receive("5678"); 
 +
 +
 + 
 +</code> 
 + 
 +**ComplexcerInterFace.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface ComplexcerInterface extends Printer, Scanner, Fax { 
 + 
 +
 + 
 +</code> 
 + 
 +**ComplexerMain3.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class ComplexerMain3 { 
 + 
 + public static void main(String[] args) { 
 +  
 + ComplexcerInterface ci = new ComplexcerInterface() { 
 + 
 + @Override 
 + public void send(String tel) { 
 + System.out.println("여기는 익명 구현 객체의 send()"); 
 +  
 +
 + 
 + @Override 
 + public void receive(String tel) { 
 + System.out.println("여기는 익명 구현 객체의 receive()"); 
 +  
 +
 + 
 + @Override 
 + public void print() { 
 + System.out.println("여기는 익명 구현 객체의 print()"); 
 +  
 +
 + 
 + @Override 
 + public void scan() { 
 + System.out.println("여기는 익명 구현 객체의 scan()"); 
 +  
 +
 +  
 + }; 
 +  
 + ci.send("1234"); 
 + ci.receive("5678"); 
 + ci.print(); 
 + ci.scan(); 
 +
 +
 + 
 +</code> 
 + 
 +===== 인터페이스의 다형성 ===== 
 + 
 + 
 +**GraphiicCard.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface GraphicCard { 
 + 
 + String MEMORY = "2G"; 
 +  
 + public void process(); 
 +  
 +
 +</code> 
 + 
 +**Amd.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class Amd implements GraphicCard { 
 + 
 + public void process() { 
 + System.out.println("AMD 그래픽 처리"); 
 +
 +  
 +
 +</code> 
 + 
 +**Nvidia.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class Nvidia implements GraphicCard { 
 + 
 + public void process() { 
 + System.out.println("Nvidia 그래픽 처리"); 
 +
 +  
 +
 +</code> 
 + 
 +**Computer.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class Computer { 
 + 
 + public static void main(String[] args) { 
 +  
 + GraphicCard gc = new Amd(); 
 +  
 + System.out.println("메모리 : "+gc.MEMORY); 
 +  
 + // Amd로 생성 
 + gc = new Amd(); // 자동 형변환 
 + gc.process(); 
 +  
 + // Nvidia로 교체 
 + gc = new Nvidia(); // 자동 형변환 
 + gc.process(); 
 +  
 +
 + 
 +
 + 
 +</code> 
 + 
 +:wr:<color #ff7f27>**매개 변수의 다형성**</color> 
 + 
 +<code java> 
 +package chapter08.poly; 
 + 
 +public class Game { 
 + 
 + void display(GraphicCard gc) { 
 + gc.process(); 
 +
 +  
 +
 +</code> 
 + 
 +  Game g = new Game(); 
 +  Amd gc = new Amd(); 
 +  g.display(gc); 
 +   
 +  Nvidia gc2 = new Nvidia(); 
 +  g.display(gc2); 
 +   
 +:wr:<color #ff7f27>**강제 형변환**</color> 
 + 
 +**Animal.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface Animal { 
 +  
 + void sleep(); 
 +
 + 
 +</code> 
 + 
 +**Eagle.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class Eagle implements Animal { 
 + 
 + public void sleep() { 
 + System.out.println("잠을 잔다."); 
 +
 +  
 + public void eat() { 
 + System.out.println("먹는다."); 
 +
 + 
 +  
 +
 + 
 +</code> 
 + 
 +**AnimalMain.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class AnimalMain { 
 + 
 + public static void main(String[] args) { 
 +  
 + Animal eagle = new Eagle(); 
 +  
 + eagle.sleep(); 
 + // eagle.eat(); // 에러 
 +  
 + Eagle eagleObj = (Eagle)eagle; // 강제 형변환 
 + eagleObj.eat(); // Eagle 클래스의 eat() 메서드 
 +  
 + System.out.println(eagle instanceof Animal); 
 +
 +
 + 
 +</code> 
 + 
 +:wr:<color #ff7f27>**instanceof**</color> 
 + 
 +  객체 isntanceof 클래스(인터페이스) 
 + 
 +**A.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface A { 
 + 
 + void a(); 
 +
 +</code> 
 + 
 +**AA.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface AA extends A { 
 + 
 + void aa(); 
 +
 + 
 +</code> 
 + 
 +**AAA.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class AAA implements AA { 
 + 
 + @Override 
 + public void a() { 
 +  
 +
 +  
 + @Override 
 + public void aa() { 
 +  
 +
 + 
 +
 + 
 +</code> 
 + 
 +**AB.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface AB extends A { 
 + 
 + void ab(); 
 +
 + 
 +</code> 
 + 
 +**ABB.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class ABB implements AB { 
 + 
 + @Override 
 + public void a() { 
 +  
 +
 + 
 + @Override 
 + public void ab() { 
 +  
 +
 + 
 +
 + 
 +</code> 
 + 
 +**InstanceofEx.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class InstanceofEx { 
 + 
 + public static void main(String[] args) { 
 + A a = new AAA(); 
 +  
 + AA aa = new AAA(); 
 + AAA aaa = new AAA(); 
 +  
 + A b = new ABB(); 
 + AB ab = new ABB(); 
 + ABB abb = new ABB(); 
 +  
 + System.out.println("a > A : " + (a instanceof A)); 
 + System.out.println("aa > A : " + (aa instanceof A)); 
 + System.out.println("aaa > A : " + (aaa instanceof A)); 
 +  
 + System.out.println("aaa > AB : " + (aaa instanceof AB)); 
 + 
 +
 + 
 +
 + 
 +</code> 
 + 
 +===== default 메서드와 static 메서드 ===== 
 + 
 + 
 +**MyInterface1.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface MyInterface1 { 
 + 
 + default void defaultMethod() { 
 + System.out.println("MyInterface1 의 default 메서드"); 
 +
 +  
 + static void staticMethod() { 
 + System.out.println("MyInterface1의 static 메서드"); 
 +
 +
 + 
 +</code> 
 + 
 +**MyInterface2.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public interface MyInterface2 { 
 + 
 + default void defaultMethod() { 
 + System.out.println("MyInterface2 의 default 메서드"); 
 +
 +  
 + static void staticMethod() { 
 + System.out.println("MyInterface2의 static 메서드"); 
 +
 +
 + 
 +</code> 
 + 
 +**Parent.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class Parent { 
 + 
 + public void method2() { 
 + System.out.println("Parent 클래스의 method2()"); 
 +
 +
 + 
 +</code> 
 + 
 +**Child.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +public class Child extends Parent implements MyInterface1, MyInterface2 { 
 + 
 + @Override 
 + public void defaultMethod() { 
 + System.out.println("Child 클래스의 default 메서드"); 
 + MyInterface1.super.defaultMethod(); 
 + MyInterface2.super.defaultMethod(); 
 +
 +  
 +  
 + 
 +
 + 
 +</code> 
 + 
 +**DefaultStaticEx.java** 
 +<code java> 
 +package chapter09; 
 + 
 +public class DefaultStaticEx { 
 + 
 + public static void main(String[] args) { 
 +  
 + Child c = new Child(); 
 + c.defaultMethod(); 
 + c.method2(); 
 +  
 + MyInterface1.staticMethod(); 
 + MyInterface2.staticMethod(); 
 + 
 +
 + 
 +
 + 
 +</code> 
 + 
 + 
 +===== 어노테이션 ===== 
 + 
 + 
 +**UserAnnot.java** 
 +<code java> 
 +package chapter09; 
 + 
 +import java.lang.annotation.Retention; 
 +import java.lang.annotation.RetentionPolicy; 
 + 
 +@Retention(RetentionPolicy.RUNTIME) 
 +public @interface UserAnnot { 
 + 
 + String value(); 
 + int number() default 5; 
 +  
 +
 + 
 +</code> 
 + 
 + 
 +**UserClass.java** 
 +<code java> 
 +package chapter09; 
 + 
 +public class UserClass { 
 + 
 + @UserAnnot(value="A"
 + public void methodA() { 
 + System.out.println("methodA() 실행"); 
 +
 +  
 + @UserAnnot(value="B", number=10) 
 + public void methodB() { 
 + System.out.println("methodB() 실행"); 
 +
 +
 + 
 +</code> 
 + 
 +**AnnotationEx.java** 
 + 
 +<code java> 
 +package chapter09; 
 + 
 +import java.lang.reflect.Method; 
 + 
 +public class AnnotationEx { 
 + 
 + public static void main(String[] args) throws Exception { 
 +  
 + Method method[] = UserClass.class.getDeclaredMethods(); 
 +  
 + for (int i=0; i<method.length; i++) { 
 + String methodName = method[i].getName(); 
 + UserAnnot annot = method[i].getAnnotation(UserAnnot.class); 
 +  
 + System.out.print(methodName + "의 어노테이션 "); 
 + System.out.print("value : "+annot.value() + " "); 
 + System.out.print("number : "+annot.number() + " "); 
 + System.out.println(); 
 +  
 + method[i].invoke(new UserClass(), null); // 메서드 실행 
 +
 + 
 +
 + 
 +
 + 
 +</code>