Definitions and methods of use

The greatest benefit is that the method can be called repeatedly, but As for those operations to be forming method, these must be based on your actual experience and self-summary of the project.
Method ## substantially defined
constraints: the definition in the main category, and the method by the main form 'method.
The method is a section of the block method may be called repeatedly. To define a method in Java, you can use the following syntax completion.

Method Name Return Type public static ([variable parameter types, ......]) {
    method body codes;
    [return [Return Value];
}

   

When defining the return value of the method has a lower of two categories:

    void: no return value;

    data type (basic type, reference type).

Example: Defining a no return value, a method without parameters

public class Demo {
    public static void main (String [] args) {
        Print (); // call a method inside a main method
    }
    public static void Print () {
        the System.out .println ( "Hello World!") ; // definition method
    }
}

example: the method defines a parameter no return value

public class Demo {
    static void main public (String [] args) {
        Print ( "the Hello"); // call the method directly inside the main method
        Print ( "World");
        Print ( "!");
    }
    public static void Print (String MSG) { // define methods
        of System.out.print (MSG);
    }
}

   

example: method returns with a parameter has values

public class Demo {
    public static void main (String [] args) {
        int the Add Result = (10, 20 is);
        the System .out.println (Result);
        // Since the method returns a value, the return value may be directly output
        System.out.println (the Add (100, 200 is));
    }
    public static int the Add (int X, Y int) {// definition methods
        return X + Y;
    }
}


However, the definition of the method when particular attention to one thing, if a void is defined on one method, the method can directly return to end the call. In general, such a process often requires a combination of return determines if implemented.

class Demo {public
    public static void main (String [] args) {
        Print (10);
        Print (20 is);
    }
    public static void Print (int X) {// definition method
        if (x == 20) // represented by the method of determination is the end of the
            return; // code after this statement is not executed
        System.out.println ( "X =" X +);
    }
}


## method overloading (focus)
method overload means: method name the same, different types and number of parameters.

Example: Implementation overloading

public class TestDemo {
    public static void main (String [] args) {
        // At this time, the body in different ways depending on the type and the number of execution parameters
        System.out.println (add (10, 20 is));
        System.out.println (the Add (30, 40, 50));
        System.out.println (the Add (1.1, 2.1));
    }
    public static int add (int X, Y int) {
        return X + Y;
    }
    // At this point the same method name, a number of different parameters
    public static int add (int X, Y int, int Z) {
        return X + Y + Z;
    }
    public static Double the Add (Double X, Y Double) {
        return X + Y;
    }
}

   
performed when there is a method overload important principle : the method requires the type of the return value must be the same.

Example: observed following procedure

public class TestDemo {
    public static void main (String [] args) {
        // At this time, the body in different ways depending on the type and the number of execution parameters
        System.out.println ( "hello"); // output string
        System.out.println (1); // output shaping
        System.out.println (1.1); // float output
        System.out.println ( 'A'); // output character
        System.out.println (true); // output Boolean
    }
}

 

proof: System.out.println () method is overridden
recursively calls

method recursive call refers to a method call their own form. If you want a general method of recursive operations often have the following characteristics:

    a method must have a closing condition of a recursive;

    method every time a recursive process must make some changes;

Example: for an accumulation operation from 1 to 100

public TestDemo {class
    public static void main (String [] args) {
        System.out.println (SUM (100));
    }
    public static int SUM (int NUM) {
        IF (NUM ==. 1)
            return. 1;
        return NUM + SUM (--num);
    }
}

 

    [main () calls] 1st execution sum () method 100 + SUM return (99);
    [sum () recursively] The second execution sum () method return 99 + sum (98 );
    [sum () recursively] The third execution sum () method return 98 + SUM (97);
    ...
    [sum () recursively] penultimate perform sum () Method return 2 + SUM (. 1);
    [sum () recursive] reciprocal 1st execution sum () method return 1;

example: 60 !, achieve this result too, should use double.

{class TestDemo public
    public static void main (String [] args) {
        System.out.println (MUL (60));
    }
    public static Double MUL (int NUM) {
        IF (NUM ==. 1)
            return. 1;
        return NUM * MUL (- NUM);
    }
}

by using the operation code reveals that in fact most of the while loop can be replaced by a recursive, recursive main reason why a lot of operations that may be performed a method, and the structure is better.

Guess you like

Origin www.cnblogs.com/-lwl/p/10958991.html