Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- event
- GIT
- 스프링부트
- class
- 메소드
- docker
- 자바스크립트
- StringBuilder
- 배열
- 프로그래머스스쿨
- thread
- SpringBoot
- Dict
- 저장소
- 객체
- array
- SSL
- Python
- c#
- 클래스
- 파이썬
- AssertJ
- Swing
- JS
- synchronized
- Java
- Docker Desktop
- join()
- JavaScript
- 자바
Archives
- Today
- Total
정리노트
[자바/java] 인터페이스 본문
interface RemoteControl {
void turnOn();
void turnOff();
}
// 인터페이스 사용시 메소드를 구현하지 않으면 에러가 발생한다.
public class Radio implements RemoteControl {
boolean on;
public void turnOn() {
on = true;
System.out.println("라디오가 켜졌습니다.");
}
public void turnOff() {
on = false;
System.out.println("라디오가 꺼졌습니다.");
}
public void status() {
System.out.println("현재의 상태: " +on);
}
public static void main(String[] args) {
Radio r = new Radio();
r.turnOn();
r.turnOff();
r.status();
RemoteControl rr = new Radio();
rr.turnOn();
rr.turnOff();
// rr.status(); // 인터페이스를 자료형으로 사용한 객체이므로 인터페이스에 없는 메소드 사용불가
}
}
interface Drivable {
int NORTH = 1; // 인터페이스 상수 생성
void drive();
default void defaultMethod() { System.out.println("디폴트 메소드는 클래스에서 구현하지 않아도 된다."); }
static void staticPrint() { System.out.println("인터페이스 내에 정적메소드 사용가능."); }
}
interface Flyable { void fly(); }
public class FlyingCar implements Drivable, Flyable {
public void drive() { System.out.println("I'm driving"); }
public void fly() { System.out.println("I'm flying"); }
public void north() { System.out.println(NORTH); } // 상수가 해당 인터페이스를 구현한다면 변수명만 사용가능
public static void main(String[] args) {
FlyingCar car = new FlyingCar();
car.drive();
car.fly();
car.north();
car.defaultMethod();
Drivable.staticPrint();
// car.staticPrint(); // 정적 메소드 - 객체로 불가
}
}
728x90
'프로그래밍 > Java' 카테고리의 다른 글
[자바/java] Swing 연습문제 숫자(UP/DOWN) 게임 (0) | 2023.01.17 |
---|---|
[자바/java] String클래스의 메소드 (0) | 2022.12.29 |
[자바/java] 물체의 운동에너지 계산(제곱, printf) (0) | 2022.11.09 |
[자바/java] 리스트의 두 값 비교하고 같은 값만 남기기 (0) | 2022.08.10 |
[자바/java] ArrayList 초기값 선언 / 대표 메소드 (0) | 2022.08.08 |