每天都进步的课堂随笔Day06
Method(方法)
-
System.out.println()// System类 out对象 println方法
-
Java methods are collections of statements, they achieve a function together
-
Method is an ordered combination of steps to solve a class of problems
-
Method is included in Class or Subject
-
Method is created in program and cited in other places
-
-
The principle to design Method: Methods are meant to be functional blocks, Is a collection of statement blocks that implement a function. When we design our methods, it is best to keep them atomic, that is, a method does only one function, which is also good for later expansion.
public class Demo01 {
public static void main(String[] args) {
int sum = add(1,2);
System.out.println(sum);
}
public static int add(int a, int b){
return a+b;
}
}
Definition of Method
-
JAVA methods are similar to functions in other languages,it is a piece of code that is used to accomplish a specific function. In general, a method contains the following syntax:
-
Method includes a method head and method body,all the parts of method as following :
-
Modifier(修饰符):this could be choose, telling editor how to call this method and defining access type of this method.
-
Return value type(返回值类型):method may return value. Some methods do not have return value, in this case, return value type is key word void.
-
Method name: The method name is the actual name of the method, method name and parameters are consisted of method signature.
-
Parameter type: The parameter acts as a placeholder to pass the value to when the method is called. This value is used as a parameter or variable. The parameter list refers to the type, order, and number of parameters of a method. Parameters are optional, and methods can contain no parameters.
-
Formal parameters(形式参数): it is used for receiving data from outside while method is called.
-
Actual parameters(实际参数):The data actually passed to the method when it is called.
-
-
Method body: method body includes detailed statements, and defines function of method.
Modifier returnValuetype methodName(parameterType parameter type){ Method body return returnValue; }
public class Demo01 { public static void main(String[] args) { int max = max(10,20); } public static int max(int num1, int num2){ int result = 0; if(num1 == num2){ System.out.println("num1 = num2"); return 0;//we usually end method by return //end method } if (num1 > num2){ result = num1; }else{ result = num2; } return result; } }
-
-
Method Call (方法调用)
-
Call Method: ObjectName. MethodName(Actual Parameters List)
-
Java support two kinds of ways to call method, depend on method return value or not
-
while method return a value, method call is to regard as a value. For example:
int laeger = max(30,40);
-
if returnValue of method is void, method call must be a statement
System.out.println("hello,Miss Fortunate");
- Just distinguish value pass(JAVA) and reference pass(区分值传递和引用传递)
Method Overload
-
-
Overload : at the same class, it has the same function name, but different formal parameters' function.(重载就是在一个类中,由相同的函数名,但形参不同的函数。)
-
Parameters List must be different( numbers of param diff / type diff / param order diff)
-
Method return type could be same or not
-
it's not enough to consist of overload if there only have difference in return type.
Achievement Theory
- if they have same method name, the compiler will select the corresponding method by matching the number and type of parameters of the method. If the matching fails, the compiler will report an error