接口中赋值的语义是什么?

What are the semantics of assignment in an interface?

我遇到了以下代码。它是一个接口,其中有属性分配。我理解接口只能用于声明方法。这样做的目的是什么?

interface Literals {
    /**
     * The meta object literal for the '{@link bowling.impl.PlayerImpl <em>Player</em>}' class.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @see bowling.impl.PlayerImpl
     * @see bowling.impl.BowlingPackageImpl#getPlayer()
     * @generated
     */
    EClass PLAYER = eINSTANCE.getPlayer();

    /**
     * The meta object literal for the '<em><b>Name</b></em>' attribute feature.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    EAttribute PLAYER__NAME = eINSTANCE.getPlayer_Name();

    /**
     * The meta object literal for the '<em><b>Date Of Birth</b></em>' attribute feature.
     * <!-- begin-user-doc -->
     * <!-- end-user-doc -->
     * @generated
     */
    EAttribute PLAYER__DATE_OF_BIRTH = eINSTANCE.getPlayer_DateOfBirth();
}

简答:常量

引用 Java Language Specification,第 9 节 "Interfaces",第 9.3 小节 "Field (Constant) Declarations":

Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.

指定隐式修饰符是一样的,但也许澄清一下:

interface Literals {
    public static final EClass PLAYER = eINSTANCE.getPlayer();
    public static final EAttribute PLAYER__NAME = eINSTANCE.getPlayer_Name();
    public static final EAttribute PLAYER__DATE_OF_BIRTH = eINSTANCE.getPlayer_DateOfBirth();
}

我称之为反模式。接口用于使用方法定义契约。在提供的接口中,它似乎用于全局变量目的。

从逻辑上讲,接口有properties/attributes是没有意义的,class应该用于具有属性和操作的实体。这可以使用 class 和出于相同目的的静态导入来完成。