Road Java based learning, - notes day8

 

Methods Summary
    concepts: method (method) is a piece of code blocks having individual functions, need to manually call to perform
    the role: You can improve the reusability and readability of the code, but will not improve operating efficiency.
    Methods must be created before you can use a process called method definition
    After creating a direct approach is not running, you need to manually use after the execution, a process known method call

 

A method of defining and calling
    custom format:
        public static void method name () {
    // method body;
}
    call format:
        Method name ();
    method must be defined, the call for the program to error
    method calls FIG memory
        method is not when invoked, all bytes in the method area code file (.class) stored
        method is invoked, to enter into the stack memory to run the
        
    case
        needs: a design method method, the method defines a variable (random value ) this variable is determined odd or even
number, and calls the method in the main method.
Thinking:
①: the definition of the method, name of method
②: The method defined variables, using the if statement is odd or even
③: main method invocation method Method
        public static void method () {
    int NUM = 10;
    if (NUM% 2 0 ==) {
        System.out.println ( "even-numbered");
    } the else {
        System.out.println ( "odd");
    }
}
public static void main(String[] args) {
    method();
}
 

The method of calling parameters and the definition of
    format definition:
        public static void method name (parameter list) {
    method body;
}
        parameter list can be composed of from 1 to a plurality of parameters, parameters used between a plurality of divided
        parameter data type and a variable name composition: data type variable names eg: int a
        time method defined parameter data type with a variable name can not be missing, missing any program error
    call format:
        method name (parameter);
        the method name (parameter 1, parameter 2) ;
        method call, the number and type of parameters must match the definition of the setting method, or program error
    parameter arguments and
        formal parameters: the full name of the formal parameter, the parameter refers to a method definition for a method call when receiving parameters.
            Format: (, parameter type 2 parameter variable name 2 parameter type 1 parameter variable name 1) method name
        arguments: full name of the actual parameter, method call parameter value of the parameter passed to the form of the parameters involved in the method code calculation
            format: method name (argument the variable name 1, argument the variable name 2) or (argument constant 1, argument constant 2)
        formal and actual parameters of the relationship
            parameter name: no relationship
                argument variable name and shape parametric variable name does not matter, can be completely different
                Are two different sets of parameters or, only when the call, assign a value to a variable argument variable parameter


            When the parameter is a reference data type
                value the value of argument variable is assigned to parameter variables, Fu fact reference type objects in the heap memory address values
                of the parameter and the reference point to the same address value heap memory by any a parameter value modification, additional details of a parameter value change occurs synchronous
    exercise
        requirements: a design method (print) for printing all of the odd n m between
        ideas:
①: definition method, entitled Print
②: the method of adding two int type parameter, the caller is ready to accept the passed arguments
③: the method of design for loop that starts from n, m to the end
④: determining if added to the cycle, is odd, the printing
⑤ : main method calls the print method, passing two actual parameters

        Code
            Package com.itheima.method2;
public class Demo2Method {
    public static void main (String [] args) {
    //. 5: main method calls the print method, two actual parameters pass
    print (20,10);
    }
    // 1: definition method, entitled Print
    // 2: Add two int type parameter as a method, solid ready to accept the passed parameters caller
    public static void Print (n-int, int m) {
        System.out.println ( n + "to" + m + "is an odd number between:");
        // 3: the method of design for loop that starts from n, m to the end
        for (int i = 20; i <= 10; i ++) {
            // 4: determining if added to the cycle, is odd, the printing
            if (I% 2 ==. 1) {
                System.out.println (I);    
            }
        }
    }
}

Return value method of defining and calling
    custom format
        public static data type method name (parameter) {
    return data;
}
        data type on the back of the return when the method defined return value and method definitions to match, otherwise the program will be given
    call format
        method name (parameters);
        data type of the variable name = name of the method (parameters); return value typically use variables received, otherwise the return value is meaningless     to practice         requirements: design a method to obtain the higher of the two numbers, the data from the the parameter         ideas             1 defines a method declaration two parameters calculated values received, and returns the results obtained 2. if statement drawn between a maximum value and B, according to the particular circumstances return results 3. in the main ( ) method call a method defined and stored using the variable []         of code             public class Demo2Method {     / *     requirements: design a method to obtain the higher of the two numbers, the parameter data from     1. define a method, two shaped declaration receiving reference calculated value, and returns the results obtained     2. if statement drawn between a maximum value and B, according to the particular circumstances return result
        












    3. Invoke the main () method using defined methods and variables to store []
    * /
    public static void main (String [] args) {
    // Invoke method defined in main () method and using { variables saved]
        System.out.println (getMax (10, 20) ); // call output
        int Result = getMax (10, 20 is);
        System.out.println (Result);
        for (int I =. 1; I <= Result; I ++) {
            System.out.println ( "the HelloWorld");
        }
    }

    // two ways to obtain a larger value of the number of
    public static int getMax (A int, int B) {
        IF (A> B) {
            return A;
        } the else {
            return B;
        }
    }
}

The method of defining and using aggregated
    methods common format
        public static return type method name (parameter list) {
    method body;
    return data;
}
            public static return type method name (int num1, int num2, Double num3) {}
        public static: modifier, to remember that the current format
        return value: returns the data type of the data after completion of the operation method of the
        method name: identifier, the method is called by a method name
        parameter list: code means before run, material needed for
        the method body : according to the business may be, is the main method of operation,
        return the role of
            1. the method for ending
            2. if any, for the data back to the caller.
        put together collectively, the method signature
    method must be defined and then using the
        process according to the requirements format, other methods to complete outer signatures in the class method and the preparation method of the definition of this process is called method
        calls the method defined according to the above-described method needs a process called at the specified location
        method for the preparation and execution order sequence nothing to do, and call in the same order.
        The code is drawn into the method can improve the reusability and readability of the code, but not efficient code
    Precautions define methods
        method signature
            method signature including the method name and parameter list does not include the return type and return value modifiers
            two methods consistent method signature: method name and parameter list exactly the same
            parameter list the same: the parameter type, number of parameters, parameter order exactly the same job, but nothing to do with the parameter name
                public static int getSum (int a, int b, C Double) {
    return 10;
} - getMax (1,2,3.0)
                public static void GetSum (int num1, int num2, Double num3) {} - getMax (1,2,3.0)
                public static void GetSum (int num1, double num3, int num2) {} - getMax (1,3.0,2)
                these two methods signatures do not match, is not the same as the order of the parameter, this might be overloaded, then speaks
                the two signatures methods consistent with the parameter unrelated
        defining methods to do two clear
            clear list of parameters: the need can not be a hundred percent clear analysis of the material before running to what type, to a few?
            Return Value Type clear: mainly clear whether there is data returned after the method of operation is completed, and if not, write void; if so, the write data corresponding to the type of
        method can not be nested definitions
            only inside a given class, other methods outside
            methods can not insert sets of definitions, but may be nested calls
                main () {
    int max = getMax ()
    Sout (max)
}
getMax () {
    int NUM = getScannerNum ();
    int num2 = getScannerNum ();
    // size comparison
}
getScannerNum () {
    // create a keyboard input object
   // open channel to get data entry keyboard
   // return the data entered
}

Return value and return value type
            method's return value can be up to a
            1. If the method does not return value
                in the method signature void return type need to write
                the method body can not write return
                method body can write return, but to write return ;
            2. If there is a return value of the method
                the method signature void return type can write
                the method signature to return return type value type body and method consistent statement returns
                body of the method must return a value of the specified type by way of return, otherwise an error
if there is a return statement criteria: from the perspective of checking whether a syntax least a return statement is executed
            3. If the method returns multiple values have
                a plurality of return method can not return values
                may be used like an array container a plurality of data is encapsulated into a container to return
                    public static int GetSum (a int, int B, C Double) {
    return 10;
} - getMax (1,2,3.0)
                    static int public [] GetSum (A int, int B, C Double) {
    int [] of arr1 = new new int [. 3]
    int [] = arr2 is new new int [. 3]
    // meal operation
   int [] [] arrDouble = new int [2] [];
    arrDouble [0] = of arr1;
    arrDouble [. 1] = arr2 is;
    return arrDouble;
} - getMax (1,2,3.0)
        return statement below, can not write code, because never reached, is not valid code
            syntax logic behind the return will not be executed, can not write other code
            , however, if a return statement will not necessarily be executed, followed by other statements can be written.
        A method, can have multiple return statements, but only execute one
            as long as you do any one of the current method is over.
            If you write more than one return statement from the syntax point of view there must be a return statement can be executed, otherwise it will error
            int getMax () {
    IF (A> b) {
        return A;
    IF the else} (A <= B) {
        return B;
    } the else {// this code can not save
        ; return B
    }
}
    Precautions method invocation
        parameter list of requirements parameters passed with the method call in full compliance with the method defined
        call format 1: method name (parameter list of matching variables / constants);
            method returns no value, can be called directly, and the only available way to call
            there are ways to return value can call, but is not recommended. Because they can not acquire the return value
        call format 2: assignment call
            data type variable name = name of the method (variable argument list match / constant);
        call format 3: Output calling
            System.out.println variable (method name (parameter list matching / constant));
        call format 4: call parameter passing
            other methods as arguments into the list
            System.out.println variable (method name (parameter list of matching / constant));
            System.out.println (getMax ( A, B));
            System.out.println (B);
        These calls only way to apply the method returns a value of
        

发布了12 篇原创文章 · 获赞 2 · 访问量 1894

Guess you like

Origin blog.csdn.net/ytzang/article/details/104428688