Java in a few simple summary about the interface

Interface also exist in our real life, the two seemingly unrelated things are connected interfaces. And interfaces in Java abstract class is a special, two different modules can be connected together, are connected via content interface, modifications more convenient

Now imagine writing a Usb Interface, users can connect the keyboard and mouse of the computer stuff.

First write a Usb Interface Usb. There is a print abstract method and a click method, representatives of the keyboard and mouse clicks, any user with an interface when there are two methods available to him.

interface Usb{
    void print();
    void click();
}

After a user needs to use the keyboard and mouse, then write a first class user interfaces to connect User1, that is, to achieve implements interface content, then rewrite abstract methods (internal interfaces are abstract methods, abstract methods must be rewritten), he is still typing click the left mouse button on the keyboard

class User1 implements Usb{
	//重写抽象方法
    public void print(){
        System.out.println("键盘输入:博客没人看");
    }

    public void click(){
        System.out.println("点了一下鼠标左键!");
    }
}

To demonstrate that more than one user can use the keyboard and mouse, that is, the interface can be connected to a number of things, write a second user User2, different from his keyboard and mouse clicks content and User1, rewrite abstract methods are also different.

class User2 implements Usb{
    //重写抽象方法和前一个用户不同
    public void print(){
        System.out.println("键盘输入了:我是用户2");
    }

    public void click(){
        System.out.println("我点了鼠标右键");
    }
}

Now the external interface has been written Well, there are two users, then a write interface to the other end of my computer, the computer interfaces to include this property to know what interface methods and call them again. Then set a general approach make, all of the methods can be called Usb interface.

class MyComputer{
    //封装一个属性
    private Usb usb;

    //给一个有参数构造方法,参数需要是Usb类型
    public MyComputer(Usb usb) {
        this.usb = usb;
    }

    //对private设置get和set方法

    public Usb getUsb() {
        return usb;
    }

    public void setUsb(Usb usb) {
        this.usb = usb;
    }

    //设置一个make方法,代表电脑可以通过Usb实现Usb内部的两个方法
    public void make(){
    	//同一个类中不需要用get和set方法
    	//也可以加上Usb usb = this.getUsb();
        usb.print();
        usb.click();
    }
}

Computer MyComputer this side is even completed, and finally a test file to test it. This amendment through the interface a shoe content is very convenient, just Usb references to different sub-class object, then a reference by creating, point to accept MyComputer objects Usb parameters, method of implementation calls.

public class PortTest06 {

    public static void main(String[] args) {
        //创建Usb引用a,父类型引用指向子类型对象User1
        Usb a = new User1();

        //创建电脑对象
        //这里传进去参数a,a是一个Usb类型数据
        MyComputer b = new MyComputer(a);
        //总方法make实现User1提供的两个方法
        b.make();

        //在创建一个Usb引用x,指向子类型对象User2
        Usb x = new User2();

        //创建电脑对象,传进去的是x
        MyComputer y = new MyComputer(x);
        //总方法make实现User2提供的两个方法
        y.make();
    }
}
Released nine original articles · won praise 1 · views 220

Guess you like

Origin blog.csdn.net/weixin_44632459/article/details/105244122