首先是接口类
public interface jiekou{
boolean powerOnOff(); //返回当前状态,关还是开
int volumeUp(int increment); //调大音量
int volumeDown(int decrement);//调低音量
void mute(); //静音
int setChannel(int channel); //返回当前频道
int channelUp(); //频道+1
int channelDown(); //频道-1
}
然后是TV类
import static java.lang.Math.max;
import static java.lang.Math.min;
public class TV implements jiekou{ //创建TV类
public TV(String user, int screensize){ //构造函数
this.user=user;
this.screensize=screensize;
}
public boolean powerOnOff(){ //实现接口powerOnOff,在TV类中
power=!power;
System.out.println(user+" "+screensize+" inch TV power "+(power? "on.":"off."));
return power;
}
public int volumeUp(int increment){ //实现接口调大音量
if(!power){
return 0;
}
volume += increment;
volume = min(volume,MAX_VOLUME);
System.out.println(user+" "+screensize+" inch TV volume level: "+volume);
return volume;
}
public int volumeDown(int decrement){ //实现接口调小音量
if(!power){
return 0;
}
volume -=decrement;
volume = max(volume,MIN_VOLUME);
System.out.println(user +" "+screensize+" inch TV volume level: "+volume);
return volume;
}
public void mute(){ //实现接口静音
if(!power){
return;
}
volume =MIN_VOLUME;
System.out.println(user+" "+screensize +" inch TV volume level: "+volume);
}
public int setChannel(int newChannel){ //实现接口返回当前频道
if(!power){
return 0;
}
if(newChannel>=MIN_CHANNEL && newChannel<=MAX_CHANNEL)
channel = newChannel;
System.out.println(user +" "+screensize +" inch TV turned to channel: "+channel);
return channel;
}
public int channelUp(){ //实现接口频道+1
if(!power){
return 0;
}
channel= channel
System.out.println(user +" "+screensize+" inch TV turned to channel: "+ channel);
return channel;
}
public int channelDown(){ //实现接口频道-1
if(!power){
return 0;
}
channel = channel>MIN_CHANNEL ? --channel : MAX_CHANNEL;
System.out.println(user +" "+screensize+" inch TV turned to channel: "+ channel);
return channel;
}
private String user = null;
private int screensize = 0;
private boolean power = false;
private final int MIN_VOLUME = 0;
private final int MAX_VOLUME = 100;
private int volume = MIN_VOLUME;
private final int MIN_CHANNEL = 0;
private final int MAX_CHANNEL = 999;
private int channel = MIN_CHANNEL;
}
下面是DVD类,它和TV类基本相似,不过少了屏幕大小的参数
import static java.lang.Math.max;
import static java.lang.Math.min;
public class DVD implements jiekou{ //创建DVD类
public DVD(String user){ //构造函数
this.user=user;
}
public boolean powerOnOff(){ //实现接口powerOnOff,在DVD类中
power=!power;
System.out.println(user+" DVD Player power "+(power? "on.":"off."));
return power;
}
public int volumeUp(int increment){ //实现接口调大音量
if(!power){
return 0;
}
volume += increment;
volume = min(volume,MAX_VOLUME);
System.out.println(user+" DVD Player volume level: "+volume);
return volume;
}
public int volumeDown(int decrement){ //实现接口调小音量
if(!power){
return 0;
}
volume -=decrement;
volume = max(volume,MIN_VOLUME);
System.out.println(user+" DVD Player volume level: "+volume);
return volume;
}
public void mute(){ //实现接口静音
if(!power){
return;
}
volume =MIN_VOLUME;
System.out.println(user+" DVD Player volume level: "+volume);
}
public int setChannel(int newChannel){ //实现接口返回当前频道
if(!power){
return 0;
}
if(newChannel>=MIN_CHANNEL && newChannel<=MAX_CHANNEL)
channel = newChannel;
System.out.println(user +" DVD Player turned to channel: "+channel);
return channel;
}
public int channelUp(){ //实现接口频道+1
if(!power){
return 0;
}
channel= channel
System.out.println(user +" DVD Player turned to channel: "+ channel);
return channel;
}
public int channelDown(){ //实现接口频道-1
if(!power){
return 0;
}
channel = channel>MIN_CHANNEL ? --channel : MAX_CHANNEL;
System.out.println(user +" DVD Player turned to channel: "+ channel);
return channel;
}
private String user = null;
private boolean power = false;
private final int MIN_VOLUME = 0;
private final int MAX_VOLUME = 100;
private int volume = MIN_VOLUME;
private final int MIN_CHANNEL = 0;
private final int MAX_CHANNEL = 999;
private int channel = MIN_CHANNEL;
}
最后是调用的Try类里面的主函数
import static java.lang.Math.random;
public class Try{
public static void main(String args[]){
jiekou jie = null;
for(int i=0 ; i<5 ; ++i){
if(random()<0.5)
jie=new TV(random()<0.5 ? "Sony" : "Hitachi",
random()<0.5 ? 46 : 40);
else
jie=new DVD(random()<0.5 ? "Panasonic": "JVC");
jie.powerOnOff();
jie.channelUp();
jie.volumeUp(10);
}
}
}
下面是结果图
心得体会:感觉和c++差别很小,难度不是太大。