public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TV haieTV = new TV();
haieTV.setChannel(5);
System.out.println("haieTV的频道是"+haieTV.getChannel());
Family zhangsanFamily = new Family();
zhangsanFamily.buyTV(haieTV);
System.out.println("zhangsanFamily开始看电视节目");
zhangsanFamily.seeTV();
int m = 2;
System.out.println("zhangsanFamily将电视更换到"+m+"频道");
zhangsanFamily.remoteContral(m);
System.out.println("haieTV的频道是"+haieTV.getChannel());
System.out.println("zhangsanFamily再看电视节目");
zhangsanFamily.seeTV();
}
}
public class Family {
TV homeTV;
void buyTV(TV tv){
homeTV=tv;
}
void remoteContral(int m){
homeTV.setChannel(m);
}
void seeTV(){
homeTV.showProgram();
}
}
public class TV {
int channel;
TV(){
channel=0;
}
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+"频道");
}
}
}
chapter4
最新推荐文章于 2024-05-18 10:44:59 发布