Frog is cry
익명클래스(day25) 본문
package day26.ex01;
public class BackPrinter implements Printable{
private int number;
@Override
public void print() {
System.out.println("흑백모드로 인쇄합니다.");
}
@Override
public void copy() {
System.out.println("흑백모드로 인쇄합니다.");
}
@Override
public void output(int number) {
if(number > 20) {
this.number = 0;
}else if(number < 1) {
this.number = 0;
}else {
this.number = number;
}
System.out.println("컬러모드로" + this.number + "매를 출력합니다.");
}
}
package day26.ex01;
public class ColorPrinter implements Printable{
private int number;
@Override
public void print() {
System.out.println("컬러모드로 인쇄합니다.");
}
@Override
public void copy() {
System.out.println("컬러모드로 복사합니다.");
}
@Override
public void output(int number) {
if(number > 20) {
this.number = 0;
}else if(number < 1) {
this.number = 0;
}else {
this.number = number;
}
System.out.println("컬러모드로" + this.number + "매를 출력합니다.");
}
}
package day26.ex01;
public interface Printable {
void print();
void copy();
void output(int number);
}
package day26.ex01;
public class Printalble_Ex {
public static void main(String[] args) {
ColorPrinter cp = new ColorPrinter();
cp.print();
cp.copy();
cp.output(3);
BackPrinter bp = new BackPrinter();
bp.print();
bp.copy();
cp.output(21);
}
}
package day26.ex01;
public class RemoteControl_Ex {
public static void main(String[] args) {
RemoteControl rc;
// RemoteControl rc = new RemoteControl(); 이와 같이 못함
rc = new Television();
rc = new Audio();
rc.turnOn();
rc.turnOff();
// 익명구현객체 실습2
Printable p1 = new Printable() {
private int number;
@Override
public void print() {
System.out.println("익명구현객체1 - 컬러모드로 인쇄합니다.");
}
@Override
public void copy() {
System.out.println("익명구현객체1 - 컬러모드로 복사합니다.");
}
@Override
public void output(int number) {
if(number > 20) {
this.number = 0;
}else if(number < 1) {
this.number = 0;
}else {
this.number = number;
}
System.out.println("익명구현객체 1- 컬러모드로" + this.number + "매를 출력합니다.");
}
};
p1.print();
p1.copy();
p1.output(10);
// 익명구현객체1 예제
RemoteControl rc1 = new RemoteControl() {
private int volume;
public void turnOn() {
System.out.println("익명구현객체1 - tv를 켭니다.");
}
public void turnOff() {
System.out.println("익명구현객체1 - tv를 끕니다.");
}
public void setVolume(int volume) {
if(volume > RemoteControl.MAX_VOLUME) {
this.volume = RemoteControl.MAX_VOLUME;
} else if(volume < RemoteControl.MIN_VOLUME) {
this.volume = RemoteControl.MIN_VOLUME;
}else {
this.volume = volume;
}
System.out.println("현재 TV 볼륨 : " + volume);
}
}; //이곳은 익명 구현 클래스의 마지막을 알리는 곳입니다. 반드시 세미콜론을 붙여야합니다.
rc1.turnOn();
// 익명구현객체2 예제
RemoteControl rc2 = new RemoteControl() {
private int volume;
public void turnOn() {
System.out.println("익명구현객체2 - tv를 켭니다.");
}
public void turnOff() {
System.out.println("익명구현객체2 - tv를 끕니다.");
}
public void setVolume(int volume) {
if(volume > RemoteControl.MAX_VOLUME) {
this.volume = RemoteControl.MAX_VOLUME;
} else if(volume < RemoteControl.MIN_VOLUME) {
this.volume = RemoteControl.MIN_VOLUME;
}else {
this.volume = volume;
}
System.out.println("현재 Audio 볼륨 : " + volume);
}
}; //이곳은 익명 구현 클래스의 마지막을 알리는 곳입니다. 반드시 세미콜론을 붙여야합니다.
rc2.turnOn();
}
}
package day26.ex01;
public interface RemoteControl {
//상수
int MAX_VOLUME = 10;
int MIN_VOLUME = 0;
//추상 메소드
void turnOn();
void turnOff();
void setVolume(int volume);
//디폴트 메소드
default void setMute(boolean mute) {
if(mute) {
System.out.println("무음 처리합니다.");
}else {
System.out.println("무음 해제합니다.");
}
}
//정적 메소드
static void changeBatterty() {
System.out.println("건전지를 교환합니다.");
}
}
package day26.ex01;
public class Television implements RemoteControl{
//필드
private int volume;
//
//turnOn() 추상 메소드의 실제 메소드
@Override
public void turnOn() {
System.out.println("TV를 켭니다.");
}
//turnOff() 추상 메소드의 실제 메소드
@Override
public void turnOff() {
System.out.println("TV를 끕니다.");
}
//setVolume() 추상 메소드의 실제 메소드
@Override
public void setVolume(int volume) {
if(volume > RemoteControl.MAX_VOLUME) {
this.volume = RemoteControl.MAX_VOLUME;
} else if(volume < RemoteControl.MIN_VOLUME) {
this.volume = RemoteControl.MIN_VOLUME;
}else {
this.volume = volume;
}
System.out.println("현재 TV 볼륨 : " + volume);
}
}
package day26.ex01;
public class Audio implements RemoteControl{
//필드
private int volume;
//
//turnOn() 추상 메소드의 실제 메소드
@Override
public void turnOn() {
System.out.println("Audio를 켭니다.");
}
//turnOff() 추상 메소드의 실제 메소드
@Override
public void turnOff() {
System.out.println("Audio를 끕니다.");
}
//setVolume() 추상 메소드의 실제 메소드
@Override
public void setVolume(int volume) {
if(volume > RemoteControl.MAX_VOLUME) {
this.volume = RemoteControl.MAX_VOLUME;
} else if(volume < RemoteControl.MIN_VOLUME) {
this.volume = RemoteControl.MIN_VOLUME;
}else {
this.volume = volume;
}
System.out.println("현재 TV 볼륨 : " + volume);
}
}
'JAVA > 국비수업' 카테고리의 다른 글
배열, 메소드 응용(Day02) (0) | 2020.08.11 |
---|---|
클래스 - (Day01) (0) | 2020.08.10 |
interface(day24) (0) | 2020.07.31 |
instanceOf(day24) (0) | 2020.07.31 |
다형성, instanceOf(Day22) (0) | 2020.07.30 |
Comments