简体   繁体   English

访问静态成员类

[英]Accessing static member class

I learnt that, to access the static member class, the syntax is OuterClass.NestedStaticClass . 我了解到,要访问静态成员类,语法为OuterClass.NestedStaticClass

For the given below interface Input , 对于下面给定的接口Input

interface Input{

    static class KeyEvent{
        public static final int KEY_DOWN = 0;
        public static final int KEY_UP = 0;
        public int type;
        public int keyCode;
        public int keyChar;
    }

    static class TouchEvent{
        public static final int TOUCH_DOWN = 0;
        public static final int TOUCH_UP =0;
        public static final int TOUCH_DRAGGED = 2;
        public int type;
        public int x, y;
        public int pointer;
    }

    public boolean isKeyPressed(int keyCode);
    .....
    public List<KeyEvent> getKeyEvents();
    public List<TouchEvent> getTouchEvents(); 
}

below is the implementation Keyboard , 下面是实现Keyboard

class Keyboard implements Input{

    ....

    @Override
    public List<TouchEvent> getTouchEvents() {
        TouchEvent obj1 = new TouchEvent();
        TouchEvent obj2 = new TouchEvent();
        List<TouchEvent> list = new ArrayList<TouchEvent>();
        list.add(obj1);
        list.add(obj2);
        return list;
    }

}

In this implementation Keyboard , I did not require to use Input.TouchEvent for below lines of code. 在此实现Keyboard ,我不需要在下面的代码行中使用Input.TouchEvent

TouchEvent obj1 = new TouchEvent();
TouchEvent obj2 = new TouchEvent();
List<TouchEvent> list = new ArrayList<TouchEvent>();

But for the below implementation, I had to use Input.TouchEvent , 但是对于以下实现,我必须使用Input.TouchEvent

public class NestedClassInInterface {
    public static void main(String[] args) {
        Input.TouchEvent t = new Input.TouchEvent();
    }
}

How do I understand this? 我怎么理解的?

From the Java Language Specification, concerning members of an interface type 来自Java语言规范, 涉及接口类型的成员

The body of an interface may declare members of the interface, that is, fields (§9.3), methods (§9.4), classes (§9.5), and interfaces (§9.5). 接口的主体可以声明接口的成员,即字段(第9.3节),方法(第9.4节),类(第9.5节)和接口(第9.5节)。

and concerning the scope of a declaration 并涉及声明范围

The scope of a declaration of a member m declared in or inherited by a class type C (§8.1.6) is the entire body of C , including any nested type declarations. 的成员的声明的范围m中声明或由类继承的类型C (§8.1.6)是整个身体C ,包括任何嵌套类型声明。

and concerning class members 关于班级成员

The members of a class type are all of the following: 类类型的成员包括以下所有:

  • [..] [..]
  • Members inherited from any direct superinterfaces (§8.1.5) 从任何直接超级接口继承的成员(第8.1.5节)

So, the type TouchEvent is declared in the type Input . 因此,类型TouchEvent在类型Input声明。 It is a member of Input . 它是Input的成员。 Keyboard implements Input and therefore inherits its members. Keyboard实现了Input ,因此继承了其成员。 TouchEvent is therefore in scope in the body of Keyboard . 因此, TouchEvent属于Keyboard范围。 You therefore don't need to qualify its use with its enclosing type. 因此,您不需要使用其封闭类型来限定其用途。

Quoted from doc : 引用自doc

The scope (§6.3) of a member (§8.2) is the entire body of the declaration of the class to which the member belongs. 成员(§8.2)的范围(§6.3)是该成员所属类的声明的整个主体。 Field, method, member class, member interface, and constructor declarations may include the access modifiers (§6.6) public, protected, or private. 字段,方法,成员类,成员接口和构造函数声明可以包括公共,受保护或私有的访问修饰符(第6.6节)。 The members of a class include both declared and inherited members (§8.2) 类的成员包括声明的成员和继承的成员 (第8.2节)

So it means class Keyboard also has members of Input in its scope and can access them directly. 因此,这意味着Keyboard类在其范围内也具有Input成员,并且可以直接访问它们。

As your class Keyboard implements interface Input it means Keyboard is Input . 当您的类Keyboard实现接口Input ,意味着Keyboard Input And it will be able to access the static nested class directly without using reference of outer interface. 而且,无需使用外部接口即可直接访问静态嵌套类。 All interface fields are also static by default but an implementation class can access them directly. 默认情况下,所有接口字段也是静态的,但是实现类可以直接访问它们。

But when you try to access it in NestedClassInInterface (which is not Input ) it needs reference of outer class/interface which is Input in this case. 但是,当您尝试在NestedClassInInterface (不是Input )中访问它时,它需要引用外部类/接口(在这种情况下为Input You can try removing implements Input from declaration of Keyboard and will see what I mean. 您可以尝试从Keyboard声明中删除implements Input ,然后会明白我的意思。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM