对象练习

public class TV {
    //定义属性
    int channel = 1;
    int vol = 1;
    boolean on =false;
    
    //构造方法:初始化
    public TV(){
        
    }
    //打开电视
    public void turnOn(){
        on = true;
    }
    
    //关闭电视
    public void turnoff(){
        on = false;
    }
    
    //设置默认频道
    public void setChannel(int newChannel){
        if(on&& newChannel>=1&&newChannel<=120){
            channel = newChannel;
        }
    }
    //设置默认声音
    public void setVol(int newVol){
        if(on&&newVol>=1&&newVol<=7){
            vol = newVol;
        }
    }
    //换频道
    public void channelUp(){
        if(on&&channel<120&&channel>0){
            channel++;
            
        }
    }
    public void channelDown(){
        if(on&&channel<120&&channel>0){
            channel--;
            
        }
    }
    //调节声音
    public void volUp(){
        if(on&&vol<7&&vol>1){
            vol++;
        }
    }
    public void volDown(){
        if(on&&vol<7&&vol>1){
            vol--;
        }
    }
}
---------------------------------------------------------------
public class Dome {
    public static void main(String[] args) {
        TV tv = new TV();
        tv.turnOn();
        tv.channelDown();
        tv.volUp();
        System.out.println(tv.channel+":"+tv.vol);
    }

}
原文地址:https://www.cnblogs.com/flei/p/6700162.html