Calling a method inside another method in same class

前端 未结 5 782
北海茫月
北海茫月 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 03:57

    It is not recursion, it is overloading. The two add methods (the one in your snippet, and the one "provided" by ArrayList that you are extending) are not the same method, cause they are declared with different parameters.

    0 讨论(0)
  • 2020-11-29 04:00

    The add method that takes a String and a Person is calling a different add method that takes a Position. The one that takes Position is inherited from the ArrayList class.

    Since your class Staff extends ArrayList<Position>, it automatically has the add(Position) method. The new add(String, Person) method is one that was written particularly for the Staff class.

    0 讨论(0)
  • 2020-11-29 04:03

    It is just an overload. The add method is from the ArrayList class. Look that Staff inherits from it.

    0 讨论(0)
  • 2020-11-29 04:11

    Recursion is a method that call itself. In this case it is a recursion. However it will be overloading until you put a restriction inside the method to stop the loop (if-condition).

    0 讨论(0)
  • 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.

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