看电视

本文通过三个Java类:TV、Family和MainClass的定义及使用,展示了类之间的交互过程。TV类代表电视机,包含设置频道、获取频道和显示当前频道的功能;Family类模拟家庭使用电视机的情景,包括购买电视机和遥控换台等操作;MainClass类为程序入口,演示了如何创建对象并调用其方法来实现电视机频道的切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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();
		
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值