Java basics-interface (interface)

Use of interface

  1. The interface is defined using interface.

  2. How to define the interface, define the members in the interface
    2.1 jdk7 and before, can only define global constants and abstract methods
    Global constants: public static final... can be omitted
    Abstract methods: public abstract… can be omitted

    2.2 jdk8: In addition to defining global constants and abstract methods, you can also define static methods and default methods

  3. The constructor cannot be defined in the interface, which means that the interface cannot be instantiated. (An abstract class can define a constructor, but it cannot be instantiated)

  4. In Java development, interfaces are implemented through classes (implements).
    If the implementation class implements all methods in the interface, then this implementation class can be instantiated, otherwise, the class is still an abstract class.

  5. Java classes can implement multiple interfaces -> to make up for the limitations of Java class single inheritance
    Format: class A extends B implements C,D

  6. Interfaces and interfaces can be inherited directly, and multiple inheritance is possible.

  7. The specific use of the interface reflects polymorphism

  8. Interface, can actually be regarded as a kind of specification

  9. Experience in the development of interface-oriented programming
    10. Create an anonymous implementation class of the interface

package com.example;

public class InterfaceTest {
    
    
    public static void main(String[] args) {
    
    
        Plane p = new Plane();
        p.fly();
    }
}

interface Flyable {
    
    
    //全局常量
    public static final int MAX_SPEED = 7900;//第一宇宙速度
    int MIN_SPEED = 1;

    //抽象方法
    public abstract void fly();

    void stop();

}

interface Attackable {
    
    
    void attack();
}

class Plane implements Flyable {
    
    
    @Override
    public void fly() {
    
    

    }

    @Override
    public void stop() {
    
    

    }
}

abstract class Rocket implements Flyable, Attackable {
    
    
    @Override
    public void fly() {
    
    

    }
}

interface AA{
    
    

}

interface BB{
    
    

}

interface CC extends AA, BB{
    
    

}

Polymorphism, norm

package com.example;

public class USBTest {
    
    
    public static void main(String[] args) {
    
    
        Computer c = new Computer();
        //1. 创建接口的非匿名实现类的非匿名对象
        Flash f = new Flash();
        c.transferData(f);

        //2. 创建接口的非匿名实现类的匿名对象
        c.transferData(new Printer());

        //3. 创建接口的匿名实现类的非匿名对象

        USB p = new USB() {
    
    
            @Override
            public void start() {
    
    
                System.out.println("p start");
            }

            @Override
            public void stop() {
    
    
                System.out.println("p stop");
            }
        };
        c.transferData(p);

        //创建接口的匿名实现类的匿名对象
        c.transferData(new USB() {
    
    
            @Override
            public void start() {
    
    
                System.out.println("start");
            }

            @Override
            public void stop() {
    
    
                System.out.println("stop");
            }
        });
    }

}

interface USB {
    
    
    void start();

    void stop();
}

class Flash implements USB {
    
    
    @Override
    public void start() {
    
    
        System.out.println("Flash start");
    }

    @Override
    public void stop() {
    
    
        System.out.println("Flash stop");
    }
}

class Printer implements USB {
    
    
    @Override
    public void start() {
    
    
        System.out.println("Printer start");
    }

    @Override
    public void stop() {
    
    
        System.out.println("Print stop");
    }
}

class Computer{
    
    

    public void transferData(USB usb){
    
    
        usb.start();
        System.out.println("transfer data");
        usb.stop();
    }
}

New features of the interface in jdk8, static methods, default methods.

public interface CompareA {
    
    
    //静态方法,public可省
    public static void method1(){
    
    
        System.out.println("compareA:北京");
    }

    //默认方法,public可省
    public default void method2(){
    
    
        System.out.println("compareB:上海");
    }

    default void method3(){
    
    
        System.out.println("compareB:上海");
    }
}

public class SubClassTest {
    
    
    public static void main(String[] args) {
    
    
        SubClass s = new SubClass();

        //s.method1();
        //1.接口中定义的静态方法,只能通过接口来调用。(为了消灭工具类)
        CompareA.method1();
        //2.通过实现类的对象,可以调用接口中的默认方法。
        //如果实现类重写了接口中的默认方法,调用时,调用的是重写以后的方法。
        s.method2();
        s.method3();
        //3. 如果子类(或实现类)继承了父类和实现的接口中声明的同名同参的方法。--> 类优先原则
        //4. 如果实现类实现了多个接口,而这些接口中定义了同名同参的默认方法。
        //那么在实现类中没有重写此方法的情况下,报错。-->接口冲突。
    }
}

class SubClass implements CompareA {
    
    

    @Override
    public void method2() {
    
    
        System.out.println("SubClass:上海");
    }
    //5. 如何在子类中调用接口中被重写的方法
    public void myMethod(){
    
    
        CompareA.super.method2();
    }
}

Guess you like

Origin blog.csdn.net/AmorFati1996/article/details/108721043