设计一个电视机类,属性包括商品编号、开关状态、音量、频道等,同时设计一些方法对电视机的状态进行控制。例如,方法应包括开/关电视机、更换频道、提高/减小音量等。要求商品编号自动生成(可以考虑为电视机类设置一个管理商品编号的静态成员变量,或者专门设置一个编号管理类)。
注意:有些成员变量需要定义为静态的(static),控制和操纵静态成员变量的方法应是静态的(static)。
public class Television {
int nextid=1;
int id;
boolean poweron;
static int volume;
int channel;
public Television(){
this.id=nextid;
this.poweron=false;
this.volume=0;
this.channel=1;
}
public void poweron(){
if(!poweron){
poweron=true;
System.out.println("已开机");
}
else{
System.out.println("关机");
}
}
public void jia(){
if(volume<100) {
volume++;
System.out.println("音量已加到"+volume);
}
else {
System.out.println("音量已达到最大值");
}
}
public void jian(){
if(volume>0){
volume--;
System.out.println("音量已减小到"+volume);
}
else{
System.out.println("音量已达到最小值");
}
}
public void change(int channel){
if(poweron){
this.channel=channel;
System.out.println("已切换到视频"+channel);
}
else{
System.out.println("请开机");
}
}
public static void main(String[] args) {
Television tv=new Television();
System.out.println("商品编号"+tv.nextid);
tv.poweron();
tv.jia();
tv.jian();
tv.change(6);
}
}