java.作业4 看电视2

代码:

class TV
{
	Channel channel;
	String name;
	TV(String name,Channel channel)
	{
		this.name = name;
		this.channel = channel;
	}
	public void setChannel(Channel channel) 
	{
		this.channel.setChannelNumber(channel.channelNumber);
		this.channel.setChannelText(channel.channelText);

	}
	public Channel getChannel()
	{
		return channel;
	}
	
	public void showProgram()
	{
		System.out.println(channel.channelText);
	}
}
class Channel
{
	int channelNumber;
	String channelText;
	Channel()
	{

	}
	public void setChannelNumber(int cNum)
	{
		channelNumber = cNum;
	}
	public int getChannelNumber()
	{
		return channelNumber;
	}
	public void setChannelText(String cText)
	{
		channelText = cText;
	}
	public String getChannelText()
	{
		return channelText;
	}
	public void changeChannel(int cNum,String cText)
	{
		channelNumber = cNum;
		channelText = cText;
	}
}
class Family
{
	TV tv;
	String name;
	Family(String name,TV tv)
	{
		this.name = name;
		this.tv = tv;

	}
	public void buyTV(TV tv)
	{
		this.tv = tv;
		System.out.println(name+"购买了电视!");
	}
	public void getTVChannel()
	{
		System.out.println(tv.name+"的频道是:"+tv.channel.channelNumber);
	}
	public void remoteControl(Channel m)
	{
		System.out.println(name+"将频道更换到"+m.channelNumber+"频道");
		tv.setChannel(m);
	}
	public void seeTV(boolean again)
	{
		if(again == true)
		{
			System.out.println(name+"又开始看电视!");
			tv.showProgram();
		}
		else
		{
			System.out.println(name+"开始看电视!");
			tv.showProgram();
		}
		
	}
}
public class familyTV1
{
	public static void main(String args[])
	{
		Channel[] channel = new Channel[5];
		for(int i = 0;i < channel.length;i++)
		{
			channel[i] = new Channel();
			channel[i].channelNumber = i+1;
			channel[i].channelText = "中央"+String.valueOf(channel[i].channelNumber);
		}
		TV haierTV = new TV("海尔",channel[0]);
		Family zhangsanFamily = new Family("张三",haierTV);
		zhangsanFamily.buyTV(haierTV);
		zhangsanFamily.seeTV(false);
		zhangsanFamily.remoteControl(channel[2]);
		zhangsanFamily.getTVChannel();
		zhangsanFamily.seeTV(true);
	}
}

 

运行结果:


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值