热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

接口小实例java

packageldb0708.xia.demo02;***手机类*@authorLenovo**publicabstractclassPhone{Stringtype;**

package ldb0708.xia.demo02;
/**
* 手机类
* @author Lenovo
*
*/
public abstract class Phone {
String type;

/**
* 打电话
*/
public abstract void call();

/**
* 发短信
*/

public abstract void sendMessasge(String msg);



}

手机的功能


package ldb0708.xia.demo02;
/**
* 拍照
* @author Lenovo
*
*/
public interface Pictture {

/**
* 拍照返回结果
*/
String photograph(); //返回一个String类型

/**
* 人脸识别
*/
boolean faceRecgnition(String face); //返回一个布尔类型得结果


}
package ldb0708.xia.demo02;
public interface MusicPlay {


/**
* 选歌播放音乐
* @param num
* @return
*/

String playMusic(int num);

}
package ldb0708.xia.demo02;
/**
* 上网
* @author Lenovo
*
*/
public interface Online {
void online();
}

 实现类 普通手机


package ldb0708.xia.demo02;
public class CommonPhone extends Phone implements Online,MusicPlay {
/**
* 传入一个int 返回一个String
*/
@Override
public String playMusic(int num) {
// TODO Auto-generated method stub
//定义music变量
String music = null;
switch (num) {

case 1:
music = "下辈子不一定还能遇到你";
break;
case 2:
music = "春娇与志明";
case 3:
music = "有何不可";
break;
default:
System.out.println("你输入得数字有误");
break;
}
return music;
}
@Override
public void online() {
// TODO Auto-generated method stub
System.out.println("上网聊天,看电影");
}
@Override
public void call() {
// TODO Auto-generated method stub
System.out.println("打电话给小姐姐");
}
@Override
public void sendMessasge(String msg) {
// TODO Auto-generated method stub
System.out.println(msg);
}
}
//测试类
package ldb0708.xia.demo02;
public class TestCommonPhone {

public static void main(String[] args) {
CommonPhone cp = new CommonPhone();
String name = "小小";
System.out.print(name);
cp.online();
System.out.println("玩累了就去听听音乐");
String s = cp.playMusic(2);
System.out.println("你点的歌是:"+s);
System.out.println("==============");
System.out.println("手机没话费了");
cp.call();
System.out.println("打不通");
cp.sendMessasge("发短息给小姐姐");

}

}

 实现类 智能手机


package ldb0708.xia.demo02;
public class SmarPhone extends Phone implements Pictture, Online,MusicPlay {





@Override
public void online() {
// TODO Auto-generated method stub
System.out.println("上网打游戏,很好玩");
}
@Override
public String photograph() {
// TODO Auto-generated method stub
return "帅脸";
}
@Override
public boolean faceRecgnition(String face) {
// TODO Auto-generated method stub
if(face.equals("帅脸")) {
return true;
}
return false;
}
@Override
public void call() {
// TODO Auto-generated method stub
System.out.println("给小小打电话");
}
@Override
public void sendMessasge(String msg) {
// TODO Auto-generated method stub
System.out.println(msg);
}
@Override
public String playMusic(int num) {
String music = null;
switch (num) {
case 1:
music = "液量之上";
break;
case 2:
music = "精忠报国";
break;
default:
break;
}
return music;
}
}
//测试类
package ldb0708.xia.demo02;
public class Test {
public static void main(String[] args) {

// TODO Auto-generated method stub
SmarPhone sp = new SmarPhone();
String s1 = sp.type = "小米手机";
System.out.println("你用的手机是:"+s1);
sp.online();
sp.call();
System.out.println("手机要人脸识别");
String s = sp.photograph();
boolean b = sp.faceRecgnition(s);
if(!b) {
System.out.println("赶紧跑");

}else {
System.out.println("手机解锁了");
}
System.out.println("人脸识别得结果是:"+b);
sp.sendMessasge("小小大大");

String s3 = sp.playMusic(2);
System.out.println("给小小点的歌是:"+s3);


}

}

 



推荐阅读
author-avatar
mobiledu2502924307
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有