如何在Java中使用抽象的、私有的、静态的、默认的接口方法?

huangapple go评论92阅读模式
英文:

How can we use abstract, private, static, default interface methods in Java?

问题

在使用Java lambdas编码时,我了解了Java接口中的默认方法。因此,我试图更深入地理解Java接口中不同类型的方法,特别是根据它们的可见性和有效调用方式。

英文:

While coding with Java lambdas I read about default methods in Java interfaces. So I tried to understand little more about the different types of methods in Java interfaces we can have. More importantly on the basis of their visibility and valid invoking.

答案1

得分: 3

Java接口目前支持抽象、静态、默认、私有方法。在JDK7之前,只有抽象方法,但在Java8中引入了静态和默认方法。私有方法是在Java9中引入的。这些方法类型之间的一些差异如下:

  • 静态、默认、私有方法有方法体,但抽象方法没有。
  • 抽象和默认方法被称为实例方法,因为它们可以被实现类调用,但是私有静态方法只能从静态方法中调用,而静态方法则使用接口名称调用。
  • 除了抽象方法外,所有其他方法类型都必须有方法体。
  • 接口方法默认为公共方法,但也可以具有私有访问权限。不允许受保护的访问权限。静态方法也可以是私有的。

让我在这里放一些代码以更好地理解它:

public interface MyInterface{

    void abstractMethod();

    private void privateMethod() {
        System.out.println("Hi, this is privateMethod");
    }

    private static void staticPrivateMethod() {
        System.out.println("Hi, this is staticPrivateMethod");
    }

    static void staticMethod() {
        //privateMethod();    // 非静态方法无法从静态上下文中引用
        System.out.println("Hi, this is staticMethod");
        staticPrivateMethod();
    }

    default void defaultMethod() {
        System.out.println("Hi, this is defaultMethod");
    }

    default void defaultMethod(int i) {
        System.out.println("Hi, this is defaultMethod with arg");
    }

}
public class MyInterfaceImpl implements MyInterface{

    public static void main(String[] args) {

        MyInterface myInterface = new MyInterfaceImpl();

        MyInterface.staticMethod();
        // myInterface.staticMethod(); // 不允许

        myInterface.defaultMethod();
        myInterface.defaultMethod(5);
        // MyInterface.defaultMethod(); // 不允许
    }

    @Override
    public void abstractMethod() {}

    @Override
    public void defaultMethod() {
        System.out.println("Hi, this is MyInterfaceImpl defaultMethod");
    }
}
英文:

Java interfaces currently support abstract, static, default, private Methods. Till JDK7 there were only abstract methods, but in Java8 static and default methods were introduced. Private methods were introduced with Java9. Some differences between these method types are as below:

  • static, default, private methods have body but abstract methods don't
  • abstract and default methods are termed as instance methods as they can be invoked by the implementing class, but the private static methods can only be invoked from static methods and static methods are invoked with interface name.
  • Apart from abstract methods all the other method types must have a body.
  • Interface methods are default public but can have private access as well. protected access is not allowed. A static method can be private as well.

Let me put some code here in order to understand it in better way:

public interface MyInterface{

    void abstractMethod();

    private void privateMethod() {
        System.out.println("Hi, this is privateMethod");
    }

    private static void staticPrivateMethod() {
        System.out.println("Hi, this is staticPrivateMethod");
    }

    static void staticMethod() {
        //privateMethod();    // Non-static method cannot be referenced from a static contex
        System.out.println("Hi, this is staticMethod");
        staticPrivateMethod();
    }

    default void defaultMethod() {
        System.out.println("Hi, this is defaultMethod");
    }

    default void defaultMethod(int i) {
        System.out.println("Hi, this is defaultMethod with arg");
    }

}

public class MyInterfaceImpl implements MyInterface{

    public static void main(String[] args) {

        MyInterface myInterface = new MyInterfaceImpl();

        MyInterface.staticMethod();
        // myInterface.staticMethod(); // Not allowed

        myInterface.defaultMethod();
        myInterface.defaultMethod(5);
        // MyInterface.defaultMethod(); // Not allowed
    }

    @Override
    public void abstractMethod() {}

    @Override
    public void defaultMethod() {
        System.out.println("Hi, this is MyInterfaceImpl defaultMethod");
    }
}

huangapple
  • 本文由 发表于 2020年7月26日 14:44:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63096969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定