/*
* 烟台大学计算机学院学生
*All right reserved.
*文件名称:java
*作者:孔云
*完成日期:2014年10月11日
*版本号:v1.0
*对任务及求解方法的描述部分:java-- 实现多个类对象组合与复用
*我的程序:
*/
Chieve.java
import java.util.*;
public class Chieve {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TV HaierTV = new TV();
Family Zhangsan = new Family();
Channel channel = new Channel();
System.out.println("张三家买的电视品牌是:");
HaierTV.brandTV();
System.out.println("张三家现在看的电视频道是:");
Zhangsan.buyTV(HaierTV);
Zhangsan.remoteControl(1);
Zhangsan.seeTV();
System.out.println("请转换频道:");
channel.getTV(HaierTV);
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();
channel.usingTV(x);
System.out.println("在线频道是:");
Zhangsan.seeTV();
}
}
Family.java
public class Family {
TV tv;
public void buyTV(TV tv) {
this.tv = tv;
}
public void remoteControl(int m){
tv.channel=m;
}
public void seeTV() {
tv.showProgram();
}
}
TV.java
public class TV {
int channel;
public int getChannel() {
return channel;
}
public void setChannel(int channel) {
this.channel = channel;
}
public void brandTV(){
System.out.println("海尔");
}
public void showProgram() {
switch (channel) {
case 1:
System.out.println("CCTV-1");
break;
case 2:
System.out.println("CCTV-2");
break;
case 3:
System.out.println("CCTV-3");
break;
case 4:
System.out.println("CCTV-4");
break;
case 5:
System.out.println("CCTV-5");
break;
case 6:
System.out.println("CCTV-6");
break;
case 7:
System.out.println("CCTV-7");
break;
case 8:
System.out.println("CCTV-8");
break;
case 9:
System.out.println("CCTV-9");
break;
case 10:
System.out.println("CCTV-10");
break;
default:
System.out.println("请换频道");
break;
}
}
}
Channel.java
public class Channel {
TV tele;
public void getTV(TV tele) {
this.tele = tele;
}
public void usingTV(int n) {
tele.channel = n;
}
}
张三家买的电视品牌是:
海尔
张三家现在看的电视频道是:
CCTV-1
请转换频道:
6
在线频道是:
CCTV-6