简体   繁体   English

在Java中调用方法时可以省略'this'吗?

[英]can be 'this' omitted when invoking method in Java?

I have silly question about Java (6/7/8) grammar - are those two snippets of method invocation always equivalent? 我对Java(6/7/8)语法有愚蠢的疑问 - 这两个方法调用片段总是等价的吗?

  1. with this this

     this.myMethod(4); 
  2. without this 没有this

     myMethod(4); 

Note: Of course the problem is about every number, type and combination of arguments 注意:当然问题在于每个参数的数量,类型和组合

Weaker statement: given program P , can I create program P' only by deleting this. 较弱的陈述:给定程序P ,我可以仅通过删除this.来创建程序P' this. in front of each and every method invocation? 在每个方法调用前面?

I've taken into account local classes, anonymous classes, inner classes and various inheritance but could have not find any contradiction. 我已经考虑了本地类,匿名类,内部类和各种继承,但可能没有发现任何矛盾。 So i believe both snippets are actually the same. 所以我相信两个片段实际上都是一样的。 Unfortunately I can't manage to find any suitable proof (eg from the official grammar). 不幸的是,我无法找到任何合适的证据(例如来自官方语法)。

Could you prove me wrong by contradiction or give me some clues for construction of the equivalence proof? 你能否通过矛盾证明我错了,或者给我一些构建等价证据的线索? Thanks a lot. 非常感谢。

EDIT: the equivalence was proven wrong (see comments below) What about the weaker statement ? 编辑:等价被证明是错误的(见下面的评论) 弱的陈述怎么样?

The Java Language Specification states Java语言规范说明

  • If the form is MethodName - that is, just an Identifier - then: 如果表单是MethodName - 即只是一个Identifier - 那么:
    • Otherwise, let T be the enclosing type declaration of which the method is a member, and let n be an integer such that T is the n'th lexically enclosing type declaration of the class whose declaration immediately contains the method invocation. 否则,让T为该方法所属的封闭类型声明,并且让n为整数,使得T是类的第n个词法封闭类型声明,其声明立即包含方法调用。 The target reference is the n'th lexically enclosing instance of this . 目标参考是词法包围的实例的第n个this

and

When used as a primary expression, the keyword this denotes a value that is a reference to the object for which the instance method or default method was invoked (§15.12), or to the object being constructed 当用作主表达式时,关键字this表示一个值,该值是对调用实例方法或默认方法的对象(第15.12节)的引用,或者是对正在构造的对象的引用

and

  • If the form is Primary . [TypeArguments] Identifier 如果表单是Primary . [TypeArguments] Identifier Primary . [TypeArguments] Identifier involved, then: Primary . [TypeArguments] Identifier涉及的Primary . [TypeArguments] Identifier ,然后:
    • Otherwise, the Primary expression is evaluated and the result is used as the target reference. 否则,将计算Primary表达式,并将结果用作目标引用。

Primary here referring to this.* . Primary在这里提到this.*

In both cases, the method is going to be resolved to the same method. 在这两种情况下,该方法将被解析为相同的方法。 Given all this information, there's is no compilable program P' that can be created from a compilable program P . 鉴于所有这些信息,没有可编译程序P'可以从可编译程序P创建。

There's at least one case where they aren't equivalent. 至少有一种情况是它们不相同。 For example, this code 例如,这段代码

void doStuff(){}
void test(){
    Runnable r = new Runnable(){
        @Override
        public void run(){
            doStuff();
        }
    };
    r.run()
}

Is perfectly valid, while this 这是完全有效的

void doStuff(){}
void test(){
    Runnable r = new Runnable(){
        @Override
        public void run(){
            this.doStuff();
        }
    };
    r.run()
}

isn't. 不是。

So if you have a method defined on a class, an anonymous object declared within that class can call it's methods without using this , but if it uses this it results in a compiler error (assuming it doesn't provide an implementation of the method name itself). 所以,如果你有一个类中定义的方法,即类中声明的匿名对象可以调用它的方法不使用this ,但如果使用this编译错误它的结果(假设它不提供方法名称的实现本身)。

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

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