接口小实例java

2021/7/13 22:08:49

本文主要是介绍接口小实例java,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

 



这篇关于接口小实例java的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程