10, and the definition of the recursive method overloading and

method

The method is used to perform some specific function snippet.
  Behavioral features and functions of the method for defining instances of the class or class implementation. The method is an abstract class and an object behavior characteristics. In object-oriented, the basic unit of the whole program is the class, method and object class is subordinate.
Statement format method

Modifier 1, 2 .. modifier method return type names (form parameter list) {
  method body;
}

  • Modifier: modifier is not necessary to write , java methods have decorated their own silent symbol of. (Default)
  • Returns: method returns data to the calling environment after it is finished.
  • Return Value Type: the type of the return value agreed in advance.
  • Method name: the method name is invoked when used, is generally called with the object name.
  • Formal parameters: when the party declared the outside world for receiving incoming data.
  • Argument: when you call the method, the method of actual data transmitted.
  • Procedure: call the method of implementation of the method is the content inside the body.

Defined methods

    /**
     * main方法也是一个方法,它是一个程序的入口
     */
    public static void main(String[] args) {
        //调用方法的时候,得先创建一个对象
        TestOne testOne = new TestOne();
        //然后用对象调用方法
        testOne.print();
        testOne.add(1, 2);
        int a = testOne.addNum(1, 2);
        System.out.println(a);  //结果为:3
    }

    //无参的方法,无返回值的方法
    void print() {
        System.out.println("这是一个无参的方法");
    }

    //有参无返回值的方法
    void add(int a, int b) {
        System.out.println(a + b);
    }

    //有参有返回值的方法
    int addNum(int a, int b) {
        return a + b;
    }

Overloaded methods

Overloaded method is a method of using the same method for a plurality of names, but the list is not the same parameters of the method, the number of parameters, the order parameters are not the same, are not the same type of parameter. Just not the same return is not overloaded.
 & esmp; overloaded method is the essence of the two methods it was not actually a method, nothing but the same name.

    /**
     * 原始方法 下面所有的方法名都一样。
     * 但是参数上的不同,可以非常多的重载方法。
     */
    int add(int a, int b) {
        return a + b;
    }

    //参数的个数不相同
    int add(int a, int b,int c) {
        return a + b + c;
    }
    
    //形参不一样
    double add(double a,int b) {
        return a + b;
    }

    //形参不一样
    double add(int a,double b) {
        return a + b;
    }

    /*
    //错误的,只有返回不相同不可以
    long add(int a, int b) {
        return a + b;
    }
    //错误的,参数名不相同也不可以
    int add(int b, int a) {
        return a + b;
    }
    */

Recursive method

Recursive nature is a method that calls itself.
  But recursion is a common solution to the problem, namely the simplification of the problem. Recursive can use some simple procedures to solve complex problems.
To use the following two points must be defined recursively

1, the definition of recursive head: that is, when not to call its own method. If no recursion head, this is a recursive infinite loop.
2, recursive body: is when you need to call their own.

Once you master the wording of these two, you can write a recursion.
Here is a small example to illustrate the recursive :( seek n multiplied order)

public static void main(String[] args) {
    long time1 = System.currentTimeMillis();    //求出程序执行前的时间
    long a = sumNum(10);                //递归的方法
    long time2 = System.currentTimeMillis();    //求出程序执行后的时间
    System.out.println("递归的结果是:" + a); //递归的结果是:3628800
    System.out.println("花费的时间是:" + (time1 - time2));//花费的时间是:1

    System.out.println("*************************");
    
    long time3 = System.currentTimeMillis();    //求出程序执行前的时间
    long b = sumNum2(10);                //循环的方法
    long time4 = System.currentTimeMillis();    //求出程序执行后的时间
    System.out.println("循环的结果是:" + a); //递归的结果是:3628800
    System.out.println("花费的时间是:" + (time3 - time4));//花费的时间是:0
}

//用递归求n的乘阶
static long sumNum(int n) {
    if (n == 1) {   //递归头
        return 1;
    } else {        //递归体
        return n * sumNum(n - 1);
    }
}

//用循环求n的乘阶
static long sumNum2(int n) {
    int result = 1; //接收计算结果
    while(n>1) {
        result *= n*(n-1);  //实现计算结果的累乘操作
        n -= 2; //每次减去2,实现数字的迭代操作
    }
    return result;
}


  This picture above shows the recursive method is a set of a party, then recursively from the head end, and then return, so that execution speed than the slower cycle.
  These two examples show that recursion is not as good as the cycle so that, because most of what can be achieved recursion cycle can be achieved, and even better than the recursive understand, and do not take up memory, does not report the wrong reason, and the cycle execution less time than the time to perform recursive time.
  But not all do not use recursion, and the right places still use recursion, but commonly used method, we use recycled solve it!




Details determine success or failure!

Personal humble opinion, if not, ask righting!

Guess you like

Origin www.cnblogs.com/xdtg/p/12339923.html
Recommended