Cases related to abstract classes and interfaces in Java

1. Code and renderings

1.Usb case

Case: Interface: Usb, implementation class: electric fan UFan and USB disk UDisk, there is a service method in the interface, and the implementation class implements it.

The code is as follows (example):

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

Insert picture description here

2. Door case

Case: The abstract door has two abstract methods, close and open; the interface lock also has two abstract methods, lockUp and openLock; the anti-theft door class TheftproofDoor inherits the door and implements the abstract method of the lock interface (4 in total) ). Write a test class and call the four abstract methods rewritten by the security door.
The code is as follows (example):

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("开锁了");
    }

}

Insert picture description here

3. Upgrade door case

Case: Add the doorbell interface DoorBell to the door case, with an abstract method of takePictures; the anti-theft door class implements the doorbell interface, write a test class, and call the overriding method.
The code is as follows (example):

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. Printer case

Case: Define paper interface paper and inkbox inkbox interface, A4 paper and B5 paper respectively realize paper interface, black and white ink cartridge and color ink cartridge realize ink cartridge interface. Define the printer class, the member attributes are two interfaces, and the corresponding set method, and the print method print. Create a test class to achieve:
1. Use black and white ink cartridges to print on A4 paper
2. Use color ink cartridges to print on B5 paper

The code is as follows (example):

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

Insert picture description here
Insert picture description here

5. Mobile phone case

Case: 1. Requirements description: The original mobile phone can send text messages and talk on the phone. With the development, mobile phones have added audio, video playback, photographing, and Internet functions. Design the program according to the idea of ​​homework 3 and the following class diagram: first, write the class and interface, refer to the structure diagram of the following class; second, write the test class, let the ordinary mobile phone play audio, send messages and make phone calls, and let the smart phone go online , Play video, take pictures, send MMS and video call.
The code is as follows (example):

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

    }
}

Insert picture description here

6. Computer case

Case: Requirements description: A computer is assembled using interface-oriented programming ideas. The main components of the computer are: CPU, hard disk, and memory: First, define the CPU interface CPU, return the CPU brand and main frequency; secondly, define the memory interface EMS , Return the capacity; again, define the hard disk interface HardDisk, return the capacity; then, write each component manufacturer to implement the CPU, EMS, and HardDisk interfaces, write computer classes, assemble the computer and display related information; the
code is as follows (example):

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

Insert picture description here

to sum up

The above is the entire content of abstract classes and interface cases, mainly using abstract classes and interface-related technologies to implement these cases.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/111300699