Method (method), also known function (function), is an independent representative of reusable functions, reflecting the affairs of behavioral characteristics, functional characteristics.
Purpose / Benefits: Reusability
1, method statement format:
[Modifier] The method return type name (parameter list []) {
Method member
}
Description:
(1) [modifiers]
(2) Return Value Type: Java can be any type of data, including basic data types and the reference data type.
When the method does not return a value, expressed using the void.
-
If the return value type of the method is void, then the method body in {} return statement may not be required.
-
If the method is not a void return type, there must return a return value in {} in the method body; statements, or the compiler is not passed.
(3) the method name:
-
Try to represent the method of function, that is intended to see to know the name of
-
From the beginning of the second word capitalized
(4) [parameter list]: said in completion of the current method, we need to "caller" Give me the extra data.
-
A list format parameter: (a data type parameter name, the data type parameter name ....)
-
The method requires the number of the "caller" when calling the method, the argument list [], the type, the sequence must correspond] [parameter list.
(5) Procedure: implement the functions of the method, preferably a method to complete a separate function.
2, call the format method:
// This method calls similar level directly using class Circle { Double RADIUS; Double Area () { return * 3.14 * RADIUS RAIDUS; } String getInfo () { return "Radius:" + radius + ", Area:" + area ( ); } } // call from another class of the object name method name ([argument list]). class Circle { Double RADIUS; Double Area () { return * 3.14 * RADIUS RAIDUS; } } class TestCircle { public static main void (String [] args) { Circle Circle new new C = (); System.out.println ( "radius:" + c.raidus + ", area:" + c.area ()); } }
-
Methods [see whether the argument list parameter list] [], the number, type, and order parameter list] [correspondence;
-
Whether or not there are parameters, do not omit the call ()
(2) whether to receive the return value
-
To see whether the method has a return value, if the void is, that there is no return value, can not be received;
-
If it is not void, it can receive, and receiving the return value of a variable of type must be compatible with the type of the return value.
Sample code and declaration called 3, the method of
(1) None None Return Value Method Reference
// This class class Demo { void test1 () { // .... } void test2 () { test1 (); // directly } } // Other types of class Demo1 { void test1 () { // .. .. } } class Demo2 { void test2 () { Demo1 Demo1 new new D = (); d.test1 (); } }
(2) method returns a value has no parameters
// This class class Circle { Double RADIUS; Double Area () { return * 3.14 * RADIUS RAIDUS; } / * String getInfo () { return "Radius:" + radius + ", Area:" + area (); // directly connected together, the return value area () method. *} / String getInfo () { Double a = Area (); // return the received value a variable return "Radius:" + radius + ", Area:" + a; } } // Other types of class Circle { Double RADIUS; Double Area () { return * 3.14 * RADIUS RAIDUS; } } class TestCircle { public static void main (String [] args) { Circle Circle new new C = (); System.out.println ( "Radius:" + c.raidus + ", Area:" + c.area ()); Double c.area a = (); // with a variable receives the return value System.out.println ( "radius:" + c.raidus + ", area:" + A); } }
3) None Return Value Method Reference
//本类 class Demo{ void test1(String str){ //.... } void test2(){ test1("hello"); } } //其他类 class Demo1{ void test1(String str){ //.... } } class Demo2{ void test2(){ Demo1 d = new Demo1(); d.test1("hello"); } }
(4) method returns a value with a reference
//本类 class Demo{ int max(int a, int b){ return a>b?a:b; } void test(){ int m = max(3,4); System.out.println("最大值:" +m); } } //其他类 class Demo1{ int max(int a, int b){ return a>b?a:b; } } class Demo2{ void test2(){ Demo1 d = new Demo1(); int m = d.max(4,5); System.out.println("最大值:" +m); } }
4, the principle of the method declaration and call
(1) method must be declared after the call
If you call the method, if the method name wrong or calling a method that does not exist, the compiler will complain
Position (2) The method must be declared in the class outer Method
Right:
Class { 1 () { } Method 2 () { } }
Examples of errors:
Class { 1 () { Method 2 () {// error } } }
Where the call (3) required by the method
Class { 1 () { calling method } }
Examples of errors:
Class { 1 () { } calling methods // position error }
Call format (4) of the method to correspond with the declaration format method
① whether to add "objects."
② Do you want to receive the return value
③ Do you want to pass parameters