外部与超级课程 [英] Outer vs. Super class

查看:88
本文介绍了外部与超级课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

super的优先级高于外级吗?

Does super has higher priority than outer class?

考虑我们有三个类:


  1. ClassA

  2. ClassB

  3. ClassB中扩展ClassA的匿名类

ClassA.java:

ClassA.java:

public class ClassA {
    protected String var = "A Var";

    public void foo() {
        System.out.println("A foo()");
    }
}

ClassB.java:

ClassB.java:

public class ClassB {
    private String var = "B Var";

    public void test() {

        new ClassA() {
            public void test() {
                foo();
                System.out.println(var);
            }
        }.test();
    }

    public void foo() {
        System.out.println("B foo()");
    }
}

当我拨打新的ClassB时()。test(),我得到以下输出(这是非常期待的):

When I call new ClassB().test(), I get the following output (which is pretty much expected):

A foo()
A Var

问题:是否定义了内部类占用的位置(方法和成员)首先从超类然后从外部类开始,还是 JVM 编译器实现依赖?我查看了JLS(§15.12.3)但找不到任何参考,也许有人指出但是我误解了一些术语?

Question: Is it defined somewhere that inner class takes (methods and members) first from the super class and then from the outer class or is it JVM compiler implementation dependent? I have looked over the JLS(§15.12.3) but couldn't find any reference for that, maybe it is pointed out there but I misunderstood some of the terms?

推荐答案

参见 6.3.1影子声明


名为的方法的声明 d n 隐藏在整个范围内 d 发生的位置处于封闭范围的任何其他方法 n 的声明of d

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.

哪个可能被解释为<的声明 foo (继承自 ClassA )隐藏名为 foo foo 点发生的整个范围内的封闭范围( ClassB ) code> foo

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."

也相关 - 15.12.1 部分:


15.12.1编译时步骤1:确定要搜索的类或接口

在编译时处理方法调用的第一步是确定要调用的方法的名称以及检查该名称方法定义的类或接口。有几种情况需要考虑,具体取决于左括号前面的表格,如下所示:

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:


  • 如果表单是 MethodName ,然后有三个子句:

    • 如果它是一个简单的名称,即只是一个标识符,那么名称方法是标识符。如果标识符出现在具有该名称的可见方法声明的范围(第6.3节)中,则必须存在该方法所属的封闭类型声明。设T是最里面的类型声明。 要搜索的类或接口是

    • 如果它是TypeName.Identifier形式的限定名称,则[...]

    • 在所有其他情况下,限定名称的格式为FieldName.Identifier;然后[...]

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

      这篇关于外部与超级课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆