Java: Methods

method

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.

Outline

The method is to accomplish a specific function block, a function known as a method

format

1. modifiers: public, static, etc.
2. Return Value Type: means for defining a return value data type
3. Method Name: the name of the method, we call convenient method
4. Parameter Type: incoming data defining parameters at the time calling method type
5. parameter name: is a variable, when receiving incoming call method parameters, method parameters are called formal parameters (parameters), it receives the argument passed is referred to the actual parameters (arguments)
6. Procedure: completion code function
7.return: the method ends and the method returns the value of the specified type
8. return value: the result of the function, the return back to the caller

Precautions

1. The method does not call does
2. The method and same level relationship is not nested definitions. However, in a method you can call other methods.
3. The method defined time use commas to separate parameters
The method is called when no further transfer of data type
5. If the method has a clear return value, a value must be brought back by a return

//示例:比较两个整数数是否相等
//在构造方法的时候,要思考该方法的返回值是什么类型,形参有几个,分别是什么
public static void main(String[] args)
{
	Scanner input=new Scanner(System.in);
	int num1=input.nextInt();
	int num2=input.nextInt();
	int b=compareNum(num1,num2);//调用方法的语句,结合格式和注意事项来理解
	System.out.println(b);	
	//我们要定义一个与返回值类型相同的变量来存储返回值,不这样做没有意义。
}
/*
该方法的功能是判断两数是否相等,返回值无非是true或者false
则返回值类型为boolean类型
要判断的两个整数由键盘输入,则需要将这两个数当作实参传入
则形参为两个int类型的变量,即整数
思考完这些我们再开始写方法
*/
public static boolean compareNum(int a,int b)
{
	if(a==b)
		return true;
	else
		return false;	
}
Process method call

In the example described above
1. First, find the entry JVM the main program begins execution sequence of statements.
2. When a call statement to execute the method, the arguments a and b num1 and num2 passed the parameter
value is then 3. a and b into the method body, b == determines a
4. The result of the determination by the return back to true or false as a result of the method invocation, and assigned to the boolean type b. the

Select the method call return value (there is a clear return value)
1. calls alone, meaningless, do not use! ! !
2. If you want to return a value for further operation, select the assignment calls.
If the return value is only output, outputs the call selection, i.e., the output value in the return method, the return type and positioning void.
If after not clear whether or not to use the return value, choose assignment calls.
In a clear return value, the recommended assignment calls.

Method overloading

In the same class, there is a method to allow more than one of the same name, as long as they are different from the parameter list, regardless of the return value.

/*
参数列表不同:
1.参数类型不同
2.参数数量不同
*/
//示例:求两数之和以及三数之和
public static void main(String[] args)	 
{
	 int num1=10;
	 int num2=15;
	 int num3=20;
	 int sum1=add(num1,num2);
	 int sum2=add(num1,num2,num3);
	 System.out.println(sum1);
	 System.out.println(sum2);
}
public static int add(int num1,int num2)	//方法1
{
 	return num1+num2;
}
public static int add(int num1,int num2,int num3)//方法2
{
 	return num1+num2+num3;
}
public static double add(int num1,double num2)	//方法3
{
	return num1+num2;
}
/*
JVM会根据参数列表来调用合适的方法
方法3中的double也可以接收int类型的实参,到底会调用方法1还是方法3?
如果出现这种情况,JVM会去选择合适的方法,因为实参num2时int类型,所以会调用方法1。
*/
//练习


void show(int a,int b,float c){}	//方法1



void show(int a,float c,int b){}	//方法2

void show(int x,int y,float z){} 	//方法3

int show(int a,float c,int b){return a;} //方法4

int show(int a,float c){return a;}	//方法5
/*
方法2,3,4,5是否与方法1重载?
方法2:发生重载,参数类型有区别,调用方法时是根据实参的顺序对应传给形参的
方法3:不发生重载,虽然参数名变了,但形参只作为一个接收实参的变量,名称没有影响
方法4:发生重载,参数类型变了
注意:方法4和方法1不发生重载,参数列表相同,重载与返回值无关
方法5:发生重载,参数个数不同
*/

Published 26 original articles · won praise 1 · views 376

Guess you like

Origin blog.csdn.net/weixin_45919908/article/details/103374671