java基础---接口
Posted 流着眼泪奔跑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java基础---接口相关的知识,希望对你有一定的参考价值。
1.为什么使用接口
防盗门可以继承门的同时又继承锁吗?不可以
如何解决这个问题,接口
2.什么是接口
语法
public interface MyInterface {
public void foo();
//其他方法
}
3.接口特性
1.接口丌可以被实例化
2.实现类必须实现接口的所有方法
3.实现类可以实现多个接口
4.接口中的变量都是静态常量
5.接口中的方法都是抽象方法
Java单继承多实现
4.如何使用接口
用Java接口来实现
1.根据需求设计方法
2.实现所有方法
3.用多态的方式使用
编码实现
举例:
public interface Usb {
//接口中的方法必须是抽象方法
public void service();
}
public class Usbfan implements Usb {
@Override
public void service() {
// TODO Auto-generated method stub
System.out.println("风扇实现USB接口");
}
}
public class UsbMouse implements Usb {
@Override
public void service() {
// TODO Auto-generated method stub
System.out.println("鼠标实现Usb接口");
}
}
public class Test {
public static void main(String[] args) {
Usbfan uf=new Usbfan();
uf.service();
UsbMouse um=new UsbMouse();
um.service();
//多态实现
Usb um1=new Usbfan();
um1.service();
}
}
5.接口表示一种能力
关心实现类有何能力,而不关心实现细节
面向接口的约定而不考虑接口的具体实现
练习
package Demo1;
public abstract class Door {
//开门
public abstract void open();
//关门
public abstract void close();
}
package Demo1;
public interface Lock {
//开锁
public void openLock ();
//关锁
public void closeLock ();
}
package Demo1;
public interface Bell{
//响铃
public void ring();
}
package Demo1;
//防盗门 继承门 实现锁
public class TheProofDoor extends Door implements Lock,Bell{
@Override
public void openLock() {
// TODO Auto-generated method stub
System.out.println("开锁...");
}
@Override
public void closeLock() {
// 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("关门...");
}
@Override
public void ring() {
// TODO Auto-generated method stub
System.out.println("响铃");
}
}
package Demo1;
public class Test {
public static void main(String[] args) {
TheProofDoor tpf=new TheProofDoor();
tpf.ring();
tpf.openLock();
tpf.open();
tpf.close();
tpf.closeLock();
}
}
防盗门是一个门 is a的关系
防盗门有一个锁 has a的关系
能力:上锁、开锁
练习2:
package demo4;
public abstract class HandSet {
private String brand;
private String type;
public HandSet(String brand, String type) {
super();
this.brand = brand;
this.type = type;
}
public HandSet() {
super();
// TODO Auto-generated constructor stub
}
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 abstract void sendInfo() ;
//打电话
public abstract void call();
//打印信息
public abstract void info();
}
package demo4;
//上网
public interface NetWork {
public void conn();
}
package demo4;
//播放音乐
public interface Play {
public void play(String content);
}
package demo4;
//拍照片
public interface TakingPictures {
public void takingPictures();
}
package demo4;
//普通手机 继承父类,实现播放音乐的接口
public class CommonHandSet extends HandSet implements Play {
@Override
public void play(String content) {
// TODO Auto-generated method stub
System.out.println("播放"+content);
}
@Override
public void sendInfo() {
// TODO Auto-generated method stub
System.out.println("发送信息");
}
@Override
public void call() {
// TODO Auto-generated method stub
System.out.println("打电话");
}
@Override
public void info() {
// TODO Auto-generated method stub
System.out.println("手机品牌"+super.getBrand());
System.out.println("手机的型号"+super.getType());
}
}
package demo4;
//智能手机 实现 上网 拍照
public class SmartHandSet extends HandSet implements NetWork,TakingPictures {
@Override
public void takingPictures() {
// TODO Auto-generated method stub
System.out.println("拍照");
}
@Override
public void conn() {
// TODO Auto-generated method stub
System.out.println("上网");
}
@Override
public void sendInfo() {
// TODO Auto-generated method stub
System.out.println("发送信息");
}
@Override
public void call() {
// TODO Auto-generated method stub
System.out.println("打电话");
}
@Override
public void info() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
System.out.println("手机品牌"+super.getBrand());
System.out.println("手机的型号"+super.getType());
}
}
package demo4;
public class Test {
public static void main(String[] args) {
CommonHandSet common=new CommonHandSet();
common.call();
common.sendInfo();
common.play("学猫叫");
System.out.println("********************");
SmartHandSet shs=new SmartHandSet();
shs.setBrand("三星 Galaxy S9");
shs.setType("128G");
shs.call();
shs.sendInfo();
shs.takingPictures();
shs.conn();
shs.info();
}
}
练习3:
package demo5;
/*
*
* 纸厂
*
* *
public interface Paper {
public String getSize();
}
package demo5;
public class A4Paper implements Paper{
@Override
public String getSize() {
// TODO Auto-generated method stub
return "A4";
}
}
package demo5;
public class B5Paper implements Paper{
@Override
public String getSize() {
// TODO Auto-generated method stub
return "B5";
}
}
package demo5;
public interface InkBox {
/*
* 墨盒厂
* */
public String getColor();
}
package demo5;
public class BlackAndWhiteInkBox implements InkBox{
@Override
public String getColor() {
// TODO Auto-generated method stub
return "黑白墨盒";
}
}
package demo5;
public class ColorInkBox implements InkBox{
@Override
public String getColor() {
// TODO Auto-generated method stub
return "彩色墨盒";
}
}
package demo5;
/*
*
* 打印
* 组装
*
*
* */
public class Printer {
/*接口的属性*/
private InkBox inkBox;
private Paper paper;
public InkBox getInkBox() {
return inkBox;
}
public void setInkBox(InkBox inkBox) {
this.inkBox = inkBox;
}
public Paper getPaper() {
return paper;
}
public void setPaper(Paper paper) {
this.paper = paper;
}
public Printer(InkBox inkBox, Paper paper) {
super();
this.inkBox = inkBox;
this.paper = paper;
}
public Printer() {
super();
// TODO Auto-generated constructor stub
}
//打印
public void work(){
System.out.println("墨盒颜色:"+inkBox.getColor()+"纸张大小:"+paper.getSize());
}
}
package demo5;
public class Test {
public static void main(String[] args) {
//初始化打印机 打印机需要 墨盒和纸张
Printer printer=new Printer();
//黑白墨盒
InkBox heibai=new BlackAndWhiteInkBox();
InkBox caise=new ColorInkBox();
//纸
Paper a4=new A4Paper();
Paper b5=new B5Paper();
//打印
printer.setInkBox(caise);
printer.setPaper(a4);
printer.work();
}
}
练习
package demo6;
public interface CPU {
public String getCpu();
}
package demo6;
public class AmdCpu implements CPU {
@Override
public String getCpu() {
// TODO Auto-generated method stub
return "型号:AMD/主频:3.8GHZ";
}
}
package demo6;
public class InterCpu implements CPU {
@Override
public String getCpu() {
// TODO Auto-generated method stub
return "品牌:酷睿;主频:4.3GHZ";
}
}
package demo6;
public interface EMS {
public String getEms();
}
package demo6;
public class EMS4G implements EMS{
@Override
public String getEms() {
// TODO Auto-generated method stub
return "4G内存";
}
}
package demo6;
public class EMS8G implements EMS{
@Override
public String getEms() {
// TODO Auto-generated method stub
return "8G内存";
}
}
package demo6;
public interface HardDisk {
//返回大小
public String getHardDisk();
}
package demo6;
public class KingstonHardDisk implements HardDisk{
@Override
public String getHardDisk() {
// TODO Auto-generated method stub
return "金士顿-1T机械硬盘,128G SSD";
}
}
package demo6;
public class SanHardDisk implements HardDisk{
@Override
public String getHardDisk() {
// TODO Auto-generated method stub
return "三星-2T机械硬盘,256G SSD";
}
}
package demo6;
public class Computer {
private CPU cpu;
private EMS ems;
private HardDisk hd;
public CPU getCpu() {
return cpu;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
public EMS getEms() {
return ems;
}
public void setEms(EMS ems) {
this.ems = ems;
}
public HardDisk getHd() {
return hd;
}
public void setHd(HardDisk hd) {
this.hd = hd;
}
public Computer(CPU cpu, EMS ems, HardDisk hd) {
super();
this.cpu = cpu;
this.ems = ems;
this.hd = hd;
}
public Computer() {
super();
// TODO Auto-generated constructor stub
}
public void produce() {
System.out.println("cpu="+cpu.getCpu());
System.out.println("硬盘="+hd.getHardDisk());
System.out.println("内存="+ems.getEms());
}
}
package demo6;
public class Test {
public static void main(String[] args) {
//new 一个对象
Computer com=new Computer();
//初始化cpu
CPU intel=new InterCpu();
CPU amd=new AmdCpu();
//硬盘
HardDisk sanxing=new SanHardDisk();
HardDisk kingston=new KingstonHardDisk();
//内存
EMS ems4g=new EMS4G();
EMS ems8g=new EMS8G();
//组装电脑
com.setCpu(intel);
com.setHd(kingston);
com.setEms(ems8g);
com.produce();
}
}
以上是关于java基础---接口的主要内容,如果未能解决你的问题,请参考以下文章