java接口的行为_java接口类型的多态行为练习

首先是接口类

public interface jiekou{

boolean powerOnOff(); //返回当前状态,关还是开

int volumeUp(int increment); //调大音量

int volumeDown(int decrement);//调低音量

void mute(); //静音

int setChannel(int channel); //返回当前频道

int channelUp(); //频道+1

int channelDown(); //频道-1

}

然后是TV类

import static java.lang.Math.max;

import static java.lang.Math.min;

public class TV implements jiekou{ //创建TV类

public TV(String user, int screensize){ //构造函数

this.user=user;

this.screensize=screensize;

}

public boolean powerOnOff(){ //实现接口powerOnOff,在TV类中

power=!power;

System.out.println(user+" "+screensize+" inch TV power "+(power? "on.":"off."));

return power;

}

public int volumeUp(int increment){ //实现接口调大音量

if(!power){

return 0;

}

volume += increment;

volume = min(volume,MAX_VOLUME);

System.out.println(user+" "+screensize+" inch TV volume level: "+volume);

return volume;

}

public int volumeDown(int decrement){ //实现接口调小音量

if(!power){

return 0;

}

volume -=decrement;

volume = max(volume,MIN_VOLUME);

System.out.println(user +" "+screensize+" inch TV volume level: "+volume);

return volume;

}

public void mute(){ //实现接口静音

if(!power){

return;

}

volume =MIN_VOLUME;

System.out.println(user+" "+screensize +" inch TV volume level: "+volume);

}

public int setChannel(int newChannel){ //实现接口返回当前频道

if(!power){

return 0;

}

if(newChannel>=MIN_CHANNEL && newChannel<=MAX_CHANNEL)

channel = newChannel;

System.out.println(user +" "+screensize +" inch TV turned to channel: "+channel);

return channel;

}

public int channelUp(){ //实现接口频道+1

if(!power){

return 0;

}

channel= channel

System.out.println(user +" "+screensize+" inch TV turned to channel: "+ channel);

return channel;

}

public int channelDown(){ //实现接口频道-1

if(!power){

return 0;

}

channel = channel>MIN_CHANNEL ? --channel : MAX_CHANNEL;

System.out.println(user +" "+screensize+" inch TV turned to channel: "+ channel);

return channel;

}

private String user = null;

private int screensize = 0;

private boolean power = false;

private final int MIN_VOLUME = 0;

private final int MAX_VOLUME = 100;

private int volume = MIN_VOLUME;

private final int MIN_CHANNEL = 0;

private final int MAX_CHANNEL = 999;

private int channel = MIN_CHANNEL;

}

下面是DVD类,它和TV类基本相似,不过少了屏幕大小的参数

import static java.lang.Math.max;

import static java.lang.Math.min;

public class DVD implements jiekou{ //创建DVD类

public DVD(String user){ //构造函数

this.user=user;

}

public boolean powerOnOff(){ //实现接口powerOnOff,在DVD类中

power=!power;

System.out.println(user+" DVD Player power "+(power? "on.":"off."));

return power;

}

public int volumeUp(int increment){ //实现接口调大音量

if(!power){

return 0;

}

volume += increment;

volume = min(volume,MAX_VOLUME);

System.out.println(user+" DVD Player volume level: "+volume);

return volume;

}

public int volumeDown(int decrement){ //实现接口调小音量

if(!power){

return 0;

}

volume -=decrement;

volume = max(volume,MIN_VOLUME);

System.out.println(user+" DVD Player volume level: "+volume);

return volume;

}

public void mute(){ //实现接口静音

if(!power){

return;

}

volume =MIN_VOLUME;

System.out.println(user+" DVD Player volume level: "+volume);

}

public int setChannel(int newChannel){ //实现接口返回当前频道

if(!power){

return 0;

}

if(newChannel>=MIN_CHANNEL && newChannel<=MAX_CHANNEL)

channel = newChannel;

System.out.println(user +" DVD Player turned to channel: "+channel);

return channel;

}

public int channelUp(){ //实现接口频道+1

if(!power){

return 0;

}

channel= channel

System.out.println(user +" DVD Player turned to channel: "+ channel);

return channel;

}

public int channelDown(){ //实现接口频道-1

if(!power){

return 0;

}

channel = channel>MIN_CHANNEL ? --channel : MAX_CHANNEL;

System.out.println(user +" DVD Player turned to channel: "+ channel);

return channel;

}

private String user = null;

private boolean power = false;

private final int MIN_VOLUME = 0;

private final int MAX_VOLUME = 100;

private int volume = MIN_VOLUME;

private final int MIN_CHANNEL = 0;

private final int MAX_CHANNEL = 999;

private int channel = MIN_CHANNEL;

}

最后是调用的Try类里面的主函数

import static java.lang.Math.random;

public class Try{

public static void main(String args[]){

jiekou jie = null;

for(int i=0 ; i<5 ; ++i){

if(random()<0.5)

jie=new TV(random()<0.5 ? "Sony" : "Hitachi",

random()<0.5 ? 46 : 40);

else

jie=new DVD(random()<0.5 ? "Panasonic": "JVC");

jie.powerOnOff();

jie.channelUp();

jie.volumeUp(10);

}

}

}

下面是结果图

0818b9ca8b590ca3270a3433284dd417.png

心得体会:感觉和c++差别很小,难度不是太大。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值