์์๊ด๊ณ๊ฐ ์๋ TV
์ค๋ฒ๋ผ์ด๋ฉ ์ ์ฉ
Override : ๋ถ๋ชจ ๋ฉ์๋์ ์๋ฒฝํ๊ฒ ๊ฐ์์ผ ํจ. (๋ฐํํ์
/๋งค๊ฐ๋ณ์/์ ๊ทผ์ ํ์)
package ์์์ด์๋TV0113;
public class InheritanceTV {
public static void main(String[] args) {
ProductTV lgTV = new ProductTV("์ฐ๋ฆฌ์ง TV");
lgTV.setPower(true);
lgTV.setVolume(35);
lgTV.setChannel(1000, true);
lgTV.viewTV();
}
}
package ์์์ด์๋TV0113;
// ProtoTypeTV๋ TV์ ๊ธฐ๋ณธ ๊ธฐ๋ฅ์ ๊ฐ์ง
public class ProtoTypeTV {
protected boolean isPower; // ์ ์ ์ค์ ๊ฐ ์ ์ฅ
protected int channel; // ์ฑ๋ ์ ๋ณด ์ ์ฅ
protected int volume; // ๋ณผ๋ฅจ ์ ๋ณด ์ ์ฅ
public ProtoTypeTV() {
this.isPower = false;
channel = 10;
volume = 10;
}
// ์์ฑ์ ์ค๋ฒ๋ก๋ฉ ๋ฐ์!
public ProtoTypeTV(boolean isPower, int channel, int volume) {
this.isPower = isPower;
this.channel = channel;
this.volume = volume;
}
public void setChannel(int cnl) {
if(cnl > 0 && cnl < 1000) {
channel = cnl;
} else System.out.println("์ฑ๋ ์ค์ ๋ฒ์๋ฅผ ๋ฒ์ด๋ฌ์ต๋๋ค.");
}
}
class ProductTV extends ProtoTypeTV {
private String name;
private boolean isInternet;
ProductTV() {
super(true, 30, 30); // ๋ถ๋ชจ์ ์์ฑ์ ํธ์ถ
name = "LG TV";
}
ProductTV(String name) {
isPower = false;
channel = 10;
volume = 10;
isInternet = false; // ๊ธฐ๋ณธ์ ์ผ๋ก๋ ์ฑ๋ ์ค์ ๋ชจ๋๋ก ๋์
this.name = name;
}
void setPower(boolean power) {
isPower = power;
String OnOff = (isPower) ? "ON" : "OFF";
}
void setVolume(int vol) {
if(vol >= 0 && vol <= 100) volume = vol;
else System.out.print("๋ณผ๋ฅจ ์ค์ ๋ฒ์๋ฅผ ๋ฒ์ด๋ฌ์ต๋๋ค.");
}
@Override
public void setChannel(int cnl) {
if(cnl > 0 && cnl < 2000) channel = cnl;
else System.out.print("Error!!!");
}
// ๋ฉ์๋ ์ค๋ฒ๋ก๋ฉ
public void setChannel(int cnl, boolean isInternet) {
if(isInternet) {
System.out.println("์ธํฐ๋ท ๋ชจ๋ ์
๋๋ค.");
this.isInternet = true;
String IntOnOff = (isInternet) ? "ON" : "OFF";
} else System.out.println("error");
}
void viewTV() {
String OnOff = (isPower) ? "ON" : "OFF";
String IntOnOff = (isInternet) ? "ON" : "OFF";
System.out.println("์ด๋ฆ : " + name);
System.out.println("์ ์ : " + OnOff);
System.out.println("์ฑ๋ : " + channel);
System.out.println("๋ณผ๋ฅจ : " + volume);
System.out.println("์ธํฐ๋ท ๋ชจ๋ : " + IntOnOff);
}
}