Java上機實踐四實驗二家中的電視

張暖心發表於2020-10-07
package com.company;

public class Main
{
    public static void main(String[] args)
    {
        TV haierTV = new TV();
        haierTV.setChannel(5);
        System.out.println(haierTV.getChannel());
        Family zhangSanFamily = new Family();
        zhangSanFamily.buyTV(haierTV);
        System.out.println("zhangSanFamily開始看電視節目");
        zhangSanFamily.seeTV();
        int m = 2;
        System.out.println("zhangSanFamily將電視更換到" + m + "頻道");
        zhangSanFamily.remoteControl(m);
        System.out.println("haierTV的頻道是" + haierTV.getChannel());
        System.out.println("zhangSanFamily在看電視節目");
        zhangSanFamily.seeTV();

    }
}

class TV
{
    int channel;
    void setChannel(int m)
    {
        if(m >= 1)
        {
            channel = m;
        }
    }
    int getChannel()
    {
        return channel;
    }
    void showProgram()
    {
        switch (channel)
        {
            case 1:
                System.out.println("綜合頻道");
                break;
            case 2:
                System.out.println("經濟頻道");
                break;
            case 3:
                System.out.println("文藝頻道");
                break;
            case 4:
                System.out.println("國際頻道");
                break;
            case 5:
                System.out.println("體育頻道");
                break;
            default:
                System.out.println("不能收看" + channel + "頻道");

        }
    }
}
class Family
{
    TV homeTV;
    void buyTV(TV tv)
    {
        homeTV = tv;
    }
    void remoteControl(int m)
    {
        homeTV.setChannel(m);
    }
    void seeTV()
    {
        homeTV.getChannel();
    }

}

 

相關文章