Calling a method inside another method in same class

前端 未结 5 811
北海茫月
北海茫月 2020-11-29 03:25

In page 428 (the chapter about Type Information) of his \"Thinking In Java, 4th Ed.\", Bruce Eckel has the following example:

public class Staff extends Arra         


        
5条回答
  •  温柔的废话
    2020-11-29 04:16

    Java implicitly assumes a reference to the current object for methods called like this. So

    // Test2.java
    public class Test2 {
        public void testMethod() {
            testMethod2();
        }
    
        // ...
    }
    

    Is exactly the same as

    // Test2.java
    public class Test2 {
        public void testMethod() {
            this.testMethod2();
        }
    
        // ...
    }
    

    I prefer the second version to make more clear what you want to do.

提交回复
热议问题