본문 바로가기
728x90
반응형

java271

[Java] 24. Interface5 (default , static) package chap8; /* * JDK8버전 이후부터 인터페이스의 멤버 메서드도 구현부를 가질수 있음. * default 메서드 : 인스턴스메서드 * static 메서드 : 클래스 메서드 */ interface Myinterface1 { void method(); default void method1() { System.out.println("Myinterface1의 default 메서드:method1"); } static void staticMethod() { System.out.println("Myinterface1의 static 메서드:staticMethod"); } } interface Myinterface2 { void method(); default void method1() { Syste.. 2022. 3. 22.
[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.
728x90
반응형