接口:
接口的方法都是公共抽象方法
使用接口的例子:
防盗门的开门关门:
创建一个门的类
package com.jiekou1; public abstract class Door { public abstract void open(); public abstract void close(); }
创建锁的接口:
package com.jiekou1; public interface Lock { public void shangSuo(); public void kaiSuo(); }
创建一个防盗门类继承门的父类
package com.jiekou1; public class FangDaoMen extends Door implements Lock { @Override public void shangSuo() { // TODO Auto-generated method stub System.out.println("插上钥匙,想右转三圈,门上锁"); } @Override public void kaiSuo() { // TODO Auto-generated method stub System.out.println("插上钥匙,向左转三圈,门开锁"); } @Override public void open() { // TODO Auto-generated method stub System.out.println("开门"); } @Override public void close() { // TODO Auto-generated method stub System.out.println("关门"); }
测试:
package com.jiekou1; public class Test { public static void main(String[] args) { FangDaoMen door = new FangDaoMen(); door.close(); door.shangSuo(); door.kaiSuo(); door.open(); } }
智能手机的功能:
手机类:
package com.jiekou2; public abstract class HandSet { private String brand; private String type; public HandSet() {} public HandSet(String brand, String type){ this.brand = brand; this.type = type; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public String getType() { return type; } public void setType(String type) { this.type = type; } //手机介绍 public void showInfo() { System.out.println("这是一台"+brand+type); } //收发短信 public abstract void sendMess(); //打电话 public abstract void call(); }
创建一个拍照接口:
package com.jiekou2; //拍照 public interface TakePhpto { void takePhotos(); }
创建一个上网接口
package com.jiekou2; //上网 public interface NextWorKing { void nextWorking(); }
创建一个播放接口
package com.jiekou2; //播放 public interface Playing { void playVideo(String name); }
创建一个普通手机类继承手机类
package com.jiekou2; //普通手机类 public class CommonPhone extends HandSet implements Playing{ public CommonPhone() {} public CommonPhone(String brand,String type) { super(brand,type); } @Override public void playVideo(String name) { // TODO Auto-generated method stub System.out.println("播放音频:《"+name+"》"); } @Override public void sendMess() { // TODO Auto-generated method stub System.out.println("发送文字短信"); } @Override public void call() { // TODO Auto-generated method stub System.out.println("语音通话"); } }
创建一个智能手机类继承手机类:
package com.jiekou2; public class ZhinengPhone extends HandSet implements TakePhpto,NextWorKing,Playing { public ZhinengPhone() {} public ZhinengPhone(String brand,String type) { super(brand,type); } @Override public void playVideo(String name) { // TODO Auto-generated method stub System.out.println("播放视频《"+name+"》"); } @Override public void nextWorking() { // TODO Auto-generated method stub System.out.println("上网"); } @Override public void takePhotos() { // TODO Auto-generated method stub System.out.println("拍照"); } @Override public void sendMess() { // TODO Auto-generated method stub System.out.println("发送文字+图片+视频的信息"); } @Override public void call() { // TODO Auto-generated method stub System.out.println("视频通话"); } }
测试:
package com.jiekou2; public class Test { public static void main(String[] args) { CommonPhone common = new CommonPhone("诺基亚" ,"g110"); common.call(); common.sendMess(); common.playVideo("钞票"); common.showInfo(); System.out.println("**********************************"); ZhinengPhone zhi = new ZhinengPhone("华为" ,"p20"); zhi.call(); zhi.sendMess(); zhi.playVideo("知否,知否"); zhi.nextWorking(); zhi.takePhotos(); zhi.showInfo(); } }