javaSE --- Methods

1. Why do you need methods

Imagine a game program, the program is running, to keep firing shells (Zombies).
Action fired artillery shells need to write 100 lines of code, you need to repeat this write 100 lines of code in place to achieve each fired artillery shells, so that the program will become very bloated, very poor readability.
In order to solve the problem of code duplication written fired artillery shells code can be extracted in a {} in, and a name for the code,
so where each fired artillery shells fired artillery shells to call this code by the name it. The above-described process, the extracted code can be viewed as a method defined in the program,
the method may be invoked when the shell needs to be transmitted.

2. What is the method

Is to accomplish a specific function block
in many languages are defined functions, but we JAVA language, we call method.

3. The method defined

	修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2 ....){
		方法体;
		return 返回值;
	}

Detailed description
(1) modifiers: more, will be detailed later. Currently using static public
(2) Return Value Type: data type defined for the return value
is a name, it exists for convenience we call the method: (3) the method name
when an incoming call the method defined: (4) Parameter Type the data type of the parameter
(5) parameter name: is a variable that receives incoming calls when the method parameters
this parameter in fact, have a professional term, is called formal parameter, its role is to receive actual parameters.
( 6) Procedure: completion code function
(7) return: return end of the process and method of the type specified value
(8) return value: is the result of the function, the return back, back to the caller

Cautions 4. The method of

A: no call does method
B: Method and same level relationship is not nested definitions
C: separated by commas method defined time parameter
D: method call when not transmitting data in Type
E: if the method has explicit return value, be sure to bring back a value of return

案例 方法调用:根据键盘录入的数据输出对应的乘法表

public class print {
    public static void main(String[] args) {
        Multable(9);
    }
    public static void Multable(int a){
        for(int x=1;x<a;x++){
            for(int y=1;y<=x;y++){
                System.out.print(x+"*"+y+"="+x*y+"      ");
            }
            System.out.println();
        }
    }
}

5. Method overloading

  • Method overloading
    in the same class, the present method allows more than one of the same name, as long as they are different from the parameter list, regardless of the return value.
  • Different parameter list:
    A: the number of parameters of the different
    B: Type different parameters
    案例:求和案例 2个整数 3个整数 4个整数
public class print {

    public static void main(String[] args) {
        //下面是针对求和方法的调用
        int sum1 = add(1,2);
        int sum2 = add(1,2,3);
        int sum3 = add(1,2,4,5);
        //下面是打印求和的结果
        System.out.println("sum1="+sum1);
        System.out.println("sum2="+sum2);
        System.out.println("sum3="+sum3);
    }

    //下面的方法实现了两个整数相加
    public static int add(int x,int y) {
        return x+y;
    }
    //下面的方法实现了三个整数相加
    public static int add(int x,int y,int z) {
        return x+y+z;
    }
    //下面的方法实现了四个整数相加
    public static int add(int x,int y,int z,int h) {
        return x+y+z+h;
    }

}

6. recursive

In the call method defined phenomenon itself

  • Recursive precautions
    must be exported, otherwise, it is dead recursive
    number can not be too much, otherwise memory overflow

6.1 Recursive problem-solving ideas and illustrations

5!
Here Insert Picture Description

6.2 seeking 5!

Case presentation:
Demand: factorial 5
循环实现

public class print {
    public static void main(String[] args) {
        //定义最终结果变量
        int jc = 1;

        for (int x = 1; x <= 5; x++) {
            jc = jc * x;
        }
        System.out.println("5的阶乘是:" + jc);
    }
}

递归实现

public class print {

        public static void main(String[] args) {
            System.out.println(factorial(5));
        }
        public static int factorial(int n){
            if(n==1){
                return 1;
            }
            return n*factorial(n-1);
        }

}
Published 39 original articles · won praise 1 · views 505

Guess you like

Origin blog.csdn.net/love_to_share/article/details/104100320