처음부터 시작하는 Java
  • interface 접점을 잡는.
    2021년 03월 26일 15시 21분 56초에 업로드 된 글입니다.
    작성자: 원2
    728x90
    반응형

    아래 코드와 연결

    interface SmartPhone {   // interface를 사용한다.
    	//interface : 추상클래스의 일종 
    	public static final int iNum1 = 100;
    	int iNum2 = 100; // public static final 생략하였지만 적용됨
    	
    	public abstract void Calling(); // 밑에도 이런식으로 만들어라
    	public void Internet();
    	public void Camera();
    	
    	static void PrintNumA() {  		// 자식 클래스에서 재 정의(Override) 불가
    		System.out.println(iNum1);
    	}
    	default void PrintNumB() {      // default : interface에 구현되어 있는 메소드 : 예외조항
    		System.out.println(iNum1);
    	}
    }
    
    class IPhone implements SmartPhone{          //implements 접점을 잡는다.
    
    	@Override
    	public void Calling() {
    		System.out.println("아이폰 전화 ...");
    	}
    
    	@Override
    	public void Internet() {
    		System.out.println("LTE...");
    	}
    
    	@Override
    	public void Camera() {
    		System.out.println("500만 화소...");
    	}
    }
    class Galaxy implements SmartPhone {
    
    	@Override
    	public void Calling() {
    		System.out.println("갤럭시 전화 ...");
    
    	}
    
    	@Override
    	public void Internet() {
    		System.out.println("LTE...");
    
    	}
    
    	@Override
    	public void Camera() {
    		System.out.println("500만 화소 ...");
    
    	}
    }
    public class Test005 {
    	public static void main(String[] args) {
    		Galaxy aGalaxy = new Galaxy(); // 인터페이스의 default 메소드 호출
    		aGalaxy.PrintNumB();
    		
    		
    	}
    	
    	public static void main4(String[] args) {		//interface의 static 메서드 호출
    		SmartPhone.PrintNumA();
    		SmartPhone.PrintNumB();
    	}
    	
    	public static void main3(String[] args) {
    		SmartPhone[] aSmartPhone = {new Galaxy(), new IPhone()};
    		// 인터페이스를 상속받은 클래스의 생성과 사용2
    	    // main2와 결과 동일
    	    // 배열을 사용하여 더 간단히 구현
    		for (int i = 0 ; i <aSmartPhone.length; ++i) {
    			aSmartPhone[i].Calling();
    			aSmartPhone[i].Internet();
    			aSmartPhone[i].Camera();     				 // 배열을 사용한 더 향상된 기법.
    		}
    	} 
    	
    	public static void main2(String[] args) {   	// 인터페이스를 상속받은 클래스의 생성과 사용1
    		Galaxy aGalaxy = new Galaxy();
    		IPhone aIPhone = new IPhone();
    		
    		aGalaxy.Calling();
    		aGalaxy.Internet();
    		aGalaxy.Camera();
    		
    		aIPhone.Calling();
    		aIPhone.Internet();
    		aIPhone.Camera();
    		
    	}
    public static void main1(String[] args) {			 // 인터페이스의 static 변수 접근
    		System.out.println(SmartPhone.iNum1);
    		System.out.println(SmartPhone.iNum2);
    		//SmartPhone.iNum1 = 300; // 에러 : final로 선언됨
    	    //SmartPhone.iNum2 = 300; // 에러 : final로 선언됨
    	}
    
    }
    
    728x90
    반응형

    'Language > Java' 카테고리의 다른 글

    내부 클래스의 제어자와 접근성 예제2  (0) 2021.03.26
    내부 클래스의 제어자와 접근성 예제1  (0) 2021.03.26
    default 와 static 예제  (0) 2021.03.26
    추상  (0) 2021.03.26
    abstract (추상적인) 추상클래스  (0) 2021.03.26
    프로토콜 protocol  (0) 2021.03.26
    댓글