public 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+"频道");
}
}
}
public class Familiy {
TV homeTV;
void buyTV(TV tv)
{
//将参数tv赋值给homeTV
homeTV=tv;
}
void remoteControl(int m)
{
homeTV.setChannel(m);
}
void seeTV()
{
homeTV.showProgram();//homeTV调用showProgram()方法
}
}
public class MainClass {
public static void main(String args[]){
TV haierTV=new TV();
//haierTV调用setChannel(int m),并向参数m传递5
haierTV.setChannel(5);
System.out.println("haierTV的频道是"+haierTV.getChannel());
Family zhangSanFamily=new Family();
//zhangSanFamily调用void buyTV(TV tv)方法,并将haierTV传递给参数TV
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();
}
}
Chapter 2
最新推荐文章于 2024-08-14 10:24:14 发布