Method (function)
: is a class skill or behavior, methods can also be called inside the method, method is to complete a specific function of the code block.
Define methods
Access modifiers return type method name (argument list) {(method statement body) code block; } Access modifiers:public printvate protectedDefault return type: is the data type of the function result.voidType with return value: Data type Return value: keywordreturn(function:1: Return value,2Return value type: an immediate end to the method) and the method to define the return type is the same method name: conform to the naming rules (small hump), see knowledge meaning, convenient we call parameters: the actual parameters: is involved in actual operation parameters: the data form is defined in the method, is used to accept the parameters of the actual parameter types: It's the data type of the parameter. It's the name of the variable. To define A method well, you have to specify two things: A. B. Argument list How many arguments do you pass and the data type of each argument:1The: method will not execute unless called2: Method and method are horizontal relationship, can not be nested (method can not write method)3: Methods are defined with commas between parameters4Method calls do not pass data types5If the method has a return value, it must have onereturnReturns a return valueCopy the code
packageJava Basics 06_ Object-oriented;public class FangFa {
String name;
String color;
/* * Access modifiers return type method name (argument list) {* (method statement body) code block; *} * /
int sum(int a,int b) {
return a+b;
}
double jian(int x,int y) {
returnx-y; }}Copy the code
packageJava Basics 06_ Object-oriented;public class FangFaMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
FangFa f =new FangFa();
f.name="Method name";
f.color="Yellow";
int a =f.sum(2.3);
System.out.println(a);/ / output 5
double d = f.jian(6.2);
System.out.println(d);/ / output 4.4}}Copy the code