Outer vs. Super class

徘徊边缘 提交于 2019-12-03 03:10:23
aioobe

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 [...]

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!