문서의 이전 판입니다!
9.2 인터페이스 선언
인터페이스의 구조
[public] interface 인터페이스명 { [public abstract] 리턴타입 추상메서드명(); [public] default 리턴타입 메서드명 (매개변수...) {...} [public] static 리턴타입 메서드명(매개변수...) {...} }
상수
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); } }
9.3 인터페이스 구현
Printer.java
package chapter09; public interface Printer { int INK = 100; void print(); }
Scanner.java
package chapter09; public interface Scanner { void scan(); }
Fax.java
package chapter09; public interface Fax { String FAX_NUMBER = "02-1234-5678"; void send(String tel); void receive(String tel); }
Complexer.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("출력 실행"); } }
ComplexerMain.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"); } }
익명구현객체
인터페이스 객체명 = new 인터페이스(){ // 인터페이스의 모든 추상 메서드 구현 };
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"); } }
ComplexcerInterFace.java
package chapter09; public interface ComplexcerInterface extends Printer, Scanner, Fax { }
ComplexerMain3.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(); } }
9.4 인터페이스의 다형성
GraphiicCard.java
package chapter09; public interface GraphicCard { String MEMORY = "2G"; public void process(); }
Amd.java
package chapter09; public class Amd implements GraphicCard { public void process() { System.out.println("AMD 그래픽 처리"); } }
Nvidia.java
package chapter09; public class Nvidia implements GraphicCard { public void process() { System.out.println("Nvidia 그래픽 처리"); } }
Computer.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(); } }
매개 변수의 다형성
package chapter08.poly; public class Game { void display(GraphicCard gc) { gc.process(); } }
Game g = new Game(); Amd gc = new Amd(); g.display(gc); Nvidia gc2 = new Nvidia(); g.display(gc2);
강제 형변환
Animal.java
package chapter09; public interface Animal { void sleep(); }
Eagle.java
package chapter09; public class Eagle implements Animal { public void sleep() { System.out.println("잠을 잔다."); } public void eat() { System.out.println("먹는다."); } }
AnimalMain.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); } }
instanceof
객체 isntanceof 클래스(인터페이스)
A.java
package chapter09; public interface A { void a(); }
AA.java
package chapter09; public interface AA extends A { void aa(); }
AAA.java
package chapter09; public class AAA implements AA { @Override public void a() { } @Override public void aa() { } }
AB.java
package chapter09; public interface AB extends A { void ab(); }
ABB.java
package chapter09; public class ABB implements AB { @Override public void a() { } @Override public void ab() { } }
InstanceofEx.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)); } }
9.5 default 메서드와 static 메서드
MyLnterface1.java
package chapter09; public interface MyInterface1 { default void defaultMethod() { System.out.println("MyInterface1 의 default 메서드"); } static void staticMethod() { System.out.println("MyInterface1의 static 메서드"); } }
MyInterface2.java
package chapter09; public interface MyInterface2 { default void defaultMethod() { System.out.println("MyInterface2 의 default 메서드"); } static void staticMethod() { System.out.println("MyInterface2의 static 메서드"); } }
Parent.java
package chapter09; public class Parent { public void method2() { System.out.println("Parent 클래스의 method2()"); } }
Child.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(); } }
DefaultStaticEx.java
package chapter09; public class DefaultStaticEx { public static void main(String[] args) { Child c = new Child(); c.defaultMethod(); c.method2(); MyInterface1.staticMethod(); MyInterface2.staticMethod(); } }
9.6 어노테이션
UserAnnot.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; }
UserClass.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() 실행"); } }
AnnotationEx.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); // 메서드 실행 } } }