Java中抽象类和接口相关案例

  • Post author:
  • Post category:java




一、代码及效果图



1.Usb案例

案例:接口:Usb,实现类:电风扇UFan和u盘UDisk,接口中有service方法,实现类予以实现。


代码如下(示例):

package work2;

public class TestU {
    public static void main(String[] args) {
        Usb u = new UDisk();
            u.service();
        u= new UFan();
            u.service();
    }
}

package work2;

public class UDisk implements Usb {
    @Override
    public void service() {
        System.out.println("u盘开始工作,传输数据中。。。。。");
    }
}

package work2;

public class UFan implements Usb {
    @Override
    public void service() {
        System.out.println("usb电风扇开始工作,为主人服务");
    }
}

package work2;

public interface Usb {
    void service();
}

在这里插入图片描述



2.门案例

案例:抽象类门有两个抽象方法,关和开;接口锁也有两个抽象方,锁门(lockUp)和开锁(openLock);防盗门类TheftproofDoor,继承门,实现锁接口的抽象方法(共4个)。编写测试类,调用防盗门重写的四个抽象方法。


代码如下(示例):

package work3;

public abstract class Door {
    abstract void close();
    abstract void open();
}

package work3;

public interface Lock {
    void lockUp();
    void openLock();
}

package work3;

public class TestDoor {
    public static void main(String[] args) {
        TheftProofDoor td = new TheftProofDoor();
        td.close();
        td.lockUp();
        td.openLock();
        td.open();
    }
}

package work3;

public class TheftProofDoor extends Door implements Lock{
    @Override
    void close() {
        System.out.println("门关了,请走");
    }

    @Override
    void open() {
        System.out.println("门开了,请进");
    }

    @Override
    public void lockUp() {
        System.out.println("锁上了");
    }

    @Override
    public void openLock() {
        System.out.println("开锁了");
    }

}

在这里插入图片描述



3.升级门案例

案例:在门的案例上添加门铃接口DoorBell,有拍照存档(takePictures)的抽象方法;防盗门类实现门铃接口,编写测试类,调用重写方法。


代码如下(示例):

package work4;

public interface Bell {
    void takePicture();
}

package work4;

public abstract class Door {
    abstract void close();
    abstract void open();
}

package work4;

public interface Lock {
    void lockUp();
    void openLock();
}

package work4;

public class TestDoor {
    public static void main(String[] args) {
        TheftProofDoor td = new TheftProofDoor();
        td.close();
        td.lockUp();
        td.openLock();
        td.open();
        td.takePicture();
    }
}

package work4;

public class TheftProofDoor extends Door implements Lock, Bell {
    @Override
    void close() {
        System.out.println("门关了,请走");
    }

    @Override
    void open() {
        System.out.println("门开了,请进");
    }

    @Override
    public void lockUp() {
        System.out.println("锁上了");
    }

    @Override
    public void openLock() {
        System.out.println("开锁了");
    }


    @Override
    public void takePicture() {
        System.out.println("咔嚓,拍照存档了");
    }
}



4.打印机案例

案例:定义纸张接口paper和墨盒inkbox接口,分别有A4纸和B5纸实现纸张接口,黑白墨盒和彩色墨盒实现墨盒接口。定义打印机类,成员属性是两个接口,和对应的set方法,以及打印方法print.创建测试类,实现:

1、使用黑白墨盒在A4纸上打印

2、使用彩色墨盒在B5纸上打印


代码如下(示例):

package work5;

public class A4Paper implements Paper {
    @Override
    public String getSize() {
        return "A4";
    }
}

package work5;

public class B5Paper implements Paper {
    @Override
    public String getSize() {
        return "B5";
    }
}

package work5;

public class BlackBok implements InkBok {
    @Override
    public String getColor() {
        return "黑白";
    }
}

package work5;

public class ColorBox implements InkBok {
    @Override
    public String getColor() {
        return "彩色";
    }
}

package work5;

public interface InkBok {
   String getColor();
}

package work5;

public interface Paper {
    String getSize();
}

package work5;

public class Printer {

    private InkBok inkBok;
    private Paper paper;

    //打印方法
    public void print(){
        System.out.println("使用"+inkBok.getColor()+"墨盒在"+paper.getSize()+"纸张上打印。");
    }

    //set和get方法
    public InkBok getInkBok() {
        return inkBok;
    }

    public void setInkBok(InkBok inkBok) {
        this.inkBok = inkBok;
    }

    public Paper getPaper() {
        return paper;
    }

    public void setPaper(Paper paper) {
        this.paper = paper;
    }
}

package work5;

public class TestPrinter {
    public static void main(String[] args) {
        //1.创建打印机对象--搬一台打印机过来
        Printer p = new Printer();
        //2.需要A4、B5纸张
        A4Paper a4 = new A4Paper();
        B5Paper b5 = new B5Paper();
        //3.需要彩色、黑白墨盒
        ColorBox color = new ColorBox();
        BlackBok blackBok=new BlackBok();
        //4.组装
        p.setPaper(a4);
        p.setInkBok(blackBok);
        //5.打印
        p.print();
    }
}

在这里插入图片描述

在这里插入图片描述



5.手机案例

案例:1.需求说明:原始的手机,可以发短信,通电话。随着发展,手机增加了音频、视频播放、拍照、上网功能。按照作业3的思路和下面的类图进行程序设计:首先,编写类及接口,参照以下类的结构图;其次,编写测试类,让普通手机播放音频、发信息和通电话,让智能手机上网、播放视频、照相、发彩信和视频通电话。


代码如下(示例):

package work01;

public class AptitudeHandset extends Handset implements Play,Network,TakePicture {
    @Override
    void sendInfo() {
        System.out.println("开始发送带图片与文字信息。。。");
    }

    @Override
    void call() {
        System.out.println("开始视频通话。。。");
    }

    @Override
    public void play(String radio) {
        System.out.println("开始播放视频《"+radio+"》");
    }

    @Override
    public void netWorkConn() {
        System.out.println("已经启动移动网络。。。。。。");
    }

    @Override
    public void takePicture() {
        System.out.println("咔嚓。。。拍照成功");
    }
}

package work01;

public class CommonHandset extends Handset implements Play {
    @Override
    void sendInfo() {
        System.out.println("开始发送文字信息。。。");
    }

    @Override
    void call() {
        System.out.println("开始语音通话。。。");
    }

    @Override
    public void play(String music) {
        System.out.println("开始播放音乐《"+music+"》");
    }
}

package work01;
// 继承    重写
public abstract class Handset {
    //属性
    private String brand;
    private String 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;
    }

    //抽象方法
    abstract void sendInfo();
    abstract void call();

    public void info(){
        System.out.println("这是一款型号为 "+type+"的 "+brand+"手机");
    }
}

package work01;

public interface Network {
    void netWorkConn();
}

package work01;

public interface Play {
    void play(String content);
}

package work01;

public interface TakePicture {
    void takePicture();
}

package work01;

public class TestHandset {
    public static void main(String[] args) {
        //普通手机
        CommonHandset h = new CommonHandset();
        h.setBrand("索尼爱立信");
        h.setType("G502C");
        h.info();
        h.play("热雪");
        h.sendInfo();
        h.call();
        //智能手机
        AptitudeHandset ah = new AptitudeHandset();
        ah.setBrand("I9100");
        ah.setType("HTC");
        ah.info();
        ah.netWorkConn();
        ah.play("小时代");
        ah.takePicture();
        ah.sendInfo();
        ah.call();

    }
}

在这里插入图片描述



6.计算机案例

案例:需求说明:采用面向接口编程思想组装一台计算机,计算机的主要组成部分有:CPU、硬盘、内存:首先,定义CPU的接口CPU,返回CPU品牌和主频;其次,定义内存的接口EMS,返回容量;再次,定义硬盘的接口HardDisk,返回容量;然后,编写各组件厂商分别实现CPU、EMS、和HardDisk接口,编写计算机类,组装计算机并显示相关信息;


代码如下(示例):

package work02.impl;


import work02.Cpu;

public class CpuImpl implements Cpu {
    @Override
    public String brand() {
        return "Inter";
    }

    @Override
    public String hz() {
        return "3.8GHZ";
    }
}

package work02.impl;


import work02.Ems;

public class EmsImpl implements Ems {
    @Override
    public int size() {
        return 4;
    }
}

package work02.impl;


import work02.HardDisk;

public class HardDiskImpl implements HardDisk {
    @Override
    public int size() {
        return 3000;
    }
}

package work02;

public class Computer {
    //接口作为成员属性并封装
    private Cpu cpu;
    private Ems ems;
    private HardDisk hardDisk;
    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 getHardDisk() {
        return hardDisk;
    }
    public void setHardDisk(HardDisk hardDisk) {
        this.hardDisk = hardDisk;
    }

    public void show(){
        System.out.println("计算机信息如下:\n Cpu的品牌是:"+cpu.brand()+"  ,主频是:"+cpu.hz()+" \n 硬盘容量是:"+hardDisk.size()+"GB \n " +
                "内存容量是:"+ems.size()+"GB");
    }
}

package work02;

public interface Cpu {
    String brand();
    String hz();
}

package work02;

public interface Ems {
    int size();
}

package work02;

public interface HardDisk {
    int size();
}

package work02;


import work02.impl.CpuImpl;
import work02.impl.EmsImpl;
import work02.impl.HardDiskImpl;

public class TestComputer {
    public static void main(String[] args) {
        Computer c = new Computer();
        c.setCpu(new CpuImpl());
        c.setEms(new EmsImpl());
        c.setHardDisk(new HardDiskImpl());
        c.show();
    }
}

在这里插入图片描述



总结

以上就是抽象类和接口案例的全部的内容,主要是利用抽象类以及接口相关的技术来实现这些案例的。



版权声明:本文为StruggleBamboo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。