Talk about interface 0.0

Table of contents

The concept of interface

interface syntax

Interface member variables and methods

use of interface

implement multiple interfaces


The concept of interface

In real life, examples of interfaces abound, such as: USB ports on laptops, power sockets, etc...

On the USB port of the computer, you can insert: U disk, mouse, keyboard...all devices that conform to the USB protocol.

On the power socket jack, you can plug in: computer, TV, rice cooker...

The interface is the public standard of behavioral norms. When everyone implements it, as long as they meet the normative standards, they can be used universally!

So how to define the interface in java?

In Java, an interface can be seen as: a public specification of multiple classes, which is a reference data type.


interface syntax

Define an interface by interface

interface IShape {
    
}

Interface member variables and methods

So what are the members of the interface?

interface IShape {
    int a = 10;
    void draw();
    
}

Careful little friends will find out - why can this draw method be written like this without abstract modification?

This is because the compiler will add the following by default if you do this:

interface IShape {
    public static final int a = 10;
    
    public abstract void draw();

}

In the compiler we can also notice that these are greyed out:

 So we can conclude:

The member variables in the interface are modified by public static final by default

The member methods in the interface are modified by public abstract by default

There cannot be ordinary methods in the interface (java8 can have default and static methods, and can have specific implementations)

interface IShape {
    public static final int a = 10;

    public abstract void draw();

    public static void test1() {

    }
    default void test2() {

    }
}

use of interface

An interface type is a reference type, which can be considered more abstract than an abstract class, so an object cannot be new. But you can use the keyword  implements to implement the interface. (To rewrite all the abstract methods in the interface)

as follows:

 So how to call it?

Interface through upcasting and dynamic binding


implement multiple interfaces

In Java, there is single inheritance between classes and classes, and a class can only have one parent class, that is, multiple inheritance is not supported in Java, but a class can implement multiple interfaces .

Let's give an example of multiple interfaces:

We first define two interfaces (running, swimming), and an abstract class (Animal) and a Dog class

The following code can be understood as: Dog is an animal with the characteristics of swimming and running

//接口1
public interface IRunning {

    void running();

}



//接口2
public interface ISwimming {

    void flying();
}



//Animal类
public abstract class Animal {
    public String name;
    public int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public abstract void eat();
}


//狗类继承Animal类实现ISwimming,IRunning接口
//可以理解为Dog是一个动物具有游泳和跑的功能
public class Dog extends Animal implements ISwimming,IRunning{

    public Dog(String name, int age) {
        super(name, age);
    }

    @Override
    public void eat() {
       
        System.out.println("吃狗粮");

    }

    @Override
    public void swimming() {
        System.out.println("游泳");

    }


    @Override
    public void running() {
        System.out.println("跑");

    }
}

The following demonstrates how to call, write a Test class call.

public class Test {

    public static void test1(Animal animal) {
        animal.eat();
    }

    public static void test2(IRunning iRunning) {
        iRunning.running();
    }

    public static void test3(ISwimming iSwimming) {
        iSwimming.swimming();
    }

    public static void main(String[] args) {
        Dog dog = new Dog("汪汪队队长",10);

        test1(dog);

        test2(dog);

        test3(dog);
    }


}

 

Guess you like

Origin blog.csdn.net/llt2997632602/article/details/130559526
0.0