Outer vs. Super class

前端 未结 2 2018
暗喜
暗喜 2021-02-05 14:00

Does super has higher priority than outer class?

Consider we have three classes:

  1. ClassA
  2. ClassB
  3. Anonymous class in ClassB that extends Cla
相关标签:
2条回答
  • 2021-02-05 14:24

    See 6.3.1 Shadowing Declarations:

    A declaration d of a method named n shadows the declarations of any other methods named n that are in an enclosing scope at the point where d occurs throughout the scope of d.

    Which may be interpreted as "the declaration of foo (inherited from ClassA) shadows the declaration of any other methods named foo that are in an enclosing scope (ClassB) at the point where foo occurs, throughout the scope of foo."

    Also relevant - section 15.12.1:

    15.12.1 Compile-Time Step 1: Determine Class or Interface to Search

    The first step in processing a method invocation at compile time is to figure out the name of the method to be invoked and which class or interface to check for definitions of methods of that name. There are several cases to consider, depending on the form that precedes the left parenthesis, as follows:

    • If the form is MethodName, then there are three subcases:
      • If it is a simple name, that is, just an Identifier, then the name of the method is the Identifier. If the Identifier appears within the scope (§6.3) of a visible method declaration with that name, then there must be an enclosing type declaration of which that method is a member. Let T be the innermost such type declaration. The class or interface to search is T.
      • If it is a qualified name of the form TypeName.Identifier, then [...]
      • In all other cases, the qualified name has the form FieldName.Identifier; then [...]
    0 讨论(0)
  • 2021-02-05 14:40

    I think you are always going to get "A var".

    This is because your test() method implementation is being defined on an anonymous subclass of A. I don't think you can access the B.var instance variable within your test() method unless you explicitly refer to the outer class using ClassB.this.var.

    0 讨论(0)
提交回复
热议问题