본문 바로가기
728x90
반응형

study/Java305

[Java] 24. Interface4 (인터페이스는 객체화가 안됨) package chap8; /* * 인터페이스는 객체화가 안됨 => 인터페이스의 객체화 */ interface Action { void action(); } abstract class Abs { abstract void mothod(); } public class InterfaceEx4 { public static void main(String[] args) { //이름없는 내부 클래스 => 일회성 객체로 사용됨. Action a = new Action() { @Override public void action() { System.out.println("Action 인터페이스 객체의 method 구현"); } }; a.action(); //Abs 클래스의 객체를 이름없는 내부클래스로 생성하기 Abs ab.. 2022. 3. 22.
[Java] 24. Interface3 (매개변수로 사용되는 인터페이스) package chap8; /* * 매개변수로 사용되는 인터페이스 */ class Unit { int hp; final int MAX; Unit(int hp) { this.hp = hp; MAX = hp; } } class AirUnit extends Unit { AirUnit(int hp) { super(hp); } } class GroundUnit extends Unit { GroundUnit(int hp) { super(hp); } } interface Repairable {} class Tank extends GroundUnit implements Repairable { Tank() { super(150);} public String toString() { return "Tank"; } } cla.. 2022. 3. 22.
[Java] 24. Interface2 (리턴타입이 인터페이스인 경우) package chap8; /* * 리턴타입이 인터페이스인 경우 */ class LazerZet implements Printerable { @Override public void print() { System.out.println("레이저 printer로 출력함"); } } class InkZet implements Printerable { @Override public void print() { System.out.println("잉크젯 printer로 출력함"); } } class PrinterManager { //Printerable 인터페이스 타입 리턴 : Printerable 인터페이스를 구현한 구현클래스의 객체 리턴. public static Printerable getPrinter(Stri.. 2022. 3. 22.
[Java] 24. Interface1 (인터페이스 예제) package chap8; /* * 인터페이스 예제 * * 인터페이스 * 1. 인터페이스의 멤버는 상수, 추상메서드, default메서드, static 메서드만 가능함. * 2. 인터페이스의 모든 멤버의 접근제한자는 public임 * 3. 객체화 불가 => 구현클래스의 객체화를 통해서 객체화됨. * 4. 클래스간의 상속은 단일상속임. * 인터페이스간의 상속은 다중상속이 가능함. * 5. 클래스와 인터페이스의 관계는 구현(implements)으로 표현한다. * => 다중구현이 가능함. */ interface Printerable { //(public static final) int INK = 100; //클래스 멤버인 상수 //public static final 생략이 가능함 //public 어디서든 접근.. 2022. 3. 22.
728x90
반응형