<pre name="code" class="java" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); "><pre name="code" class="java" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); ">TV.java
<pre name="code" class="java">public class TV {
int channel;
void setChannel(int m){
if(m>1)
channel=m;
}
int getChannel(){
return channel;
}
void show(){
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;
case 6:System.out.println("娱乐频道");break;
case 7:System.out.println("电影频道");break;
default:
System.out.println("不能收看"+channel+"频道");
}
}
}
Family.java
public class Family {
TV homeTV;
void buyTV(TV tv){
homeTV=tv;//将参数tv赋值给homeTV
}
void remouteControl(int m){
homeTV.setChannel(m);
}
void seeTV(){
homeTV.show();//homeTV调用show()方法
}
}
MainClass.java
public class MainClass {
public static void main(String args[]){
TV HairTV=new TV();
HairTV.setChannel(3); //haierTV调用getChannel(int m),并向参数m传递3
// System.out.println("HairTV的频道是"+HairTV.getChannel());
Family chenMingFamily=new Family();
chenMingFamily.buyTV(HairTV);//chenMingFamily调用void buyTV(TV TV)方法,并将haierTV传递给参数TV
System.out.println("陈名家开始看电视节目");
chenMingFamily.seeTV();
int m=4;
System.out.println("陈名将电视更换到"+m+"频道");
chenMingFamily.remouteControl(m);
// System.out.println("HairTV现在的频道是"+HairTV.getChannel());
System.out.println("陈名家正在看电视节目");
chenMingFamily.seeTV();
}
}