39. Java-interface interface

1.interface

An interface is an agreement on a rule, which is an abstraction of behavior.
insert image description here

2. Definition and use of interface

insert image description here

3. Characteristics of members in the interface

insert image description here

4. The relationship between interface and class

insert image description here

5. Examples

Write standard javabean classes with interfaces and abstract classes
insert image description here

Idea 1:
insert image description here

Idea 2:
insert image description here
Comparing the two ideas, the second one is more reasonable and clearer. So the following code adopts this implementation method.

package com.zjut.interfaceTest;

//因为现在外界直接创建顶层person对象没有意义,所以不想让外界直接创建人的对象
//因此,可以把Person写完抽象的类
public abstract class Person {
    
    
    private String name;
    private int age;

    public Person() {
    
    
    }

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

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public int getAge() {
    
    
        return age;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

package com.zjut.interfaceTest;

public abstract class Coach extends Person{
    
    
    public Coach() {
    
    
    }

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

    public abstract void coach();
}

package com.zjut.interfaceTest;

public abstract class Sporter extends Person{
    
    

    public Sporter() {
    
    
    }

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

    public abstract void study();
}

package com.zjut.interfaceTest;

public interface English {
    
    
    public abstract void speakEnglish();
}

package com.zjut.interfaceTest;

public class PingpangSporter extends Sporter implements English{
    
    
    public PingpangSporter() {
    
    
    }

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

    @Override
    public void speakEnglish() {
    
    
         System.out.println("乒乓球运动员说英语");
    }

    @Override
    public void study() {
    
    
      System.out.println("乒乓球运动员学习如何打乒乓球");
    }
}

package com.zjut.interfaceTest;

public class PingpangCoach extends Coach implements English{
    
    
    public PingpangCoach() {
    
    
    }

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

    @Override
    public void coach() {
    
    
        System.out.println("乒乓球教练教授如何打乒乓球");
    }

    @Override
    public void speakEnglish() {
    
    
        System.out.println("乒乓球教练说英语");
    }


}

package com.zjut.interfaceTest;

public class BasketballSporter extends Sporter{
    
    
    public BasketballSporter() {
    
    
    }

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

    @Override
    public void study() {
    
    
        System.out.println("篮球运动员学习如何打篮球");
    }
}

package com.zjut.interfaceTest;

public class BasketballCoach extends Coach{
    
    
    public BasketballCoach() {
    
    
    }

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

    @Override
    public void coach() {
    
    
        System.out.println("篮球教练教授如何打篮球");
    }
}

6. New methods in the interface

6.1 Two new methods added after JDK8

default method, static method

6.1.1 Allow default methods to be defined in interfaces

insert image description here

6.1.2 Allow static methods to be defined in interfaces

insert image description here

6.2 New methods added after JDK9

Allows adding private methods to interfaces

insert image description here

6.3 Summary

insert image description here

7. Interface summary

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/u014217137/article/details/130280750