代码:
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);
}
}
运行结果: