java self - Methods

On section describes the flow control statements, a complex business logic will be a lot of java code components, it contains many features. For example, a shopping service, contains selected merchandise, orders, payments and other functions, if the code is written to these features together, will look very bloated, very good readability. java provides a way to solve this problem is to encapsulate a function. In the previous example is a main method, main entrance java program method, the method can call the method.

Methods Format:

Modifier return type method name (parameter list) { 
... 
return result; 
} 

modifiers: such as public  static   
Return Value Type: data type of the result of running the method, the method will return the results of 
the list of parameters: the required calculation method parameters passed when calling the method 
return : return a result after performing the method, the method proceeds to return , the end of the overall method of operating 
}

Example:

public static void main(String[] args) {
System.out.println(getSum(1,2));
}
public static int getSum(int a,int b) {
return a + b;
}

getSum is defined by calculation a, b the addition result of the method, the main function is called, a, b are parameters of the method, as modifiers public, static method is defined as a static method, int return type of the method, the return value is described int type, corresponding to the type of return to return. The main function void return type, neither value is returned, there is no return, direct output to the console.

Modifiers:
Offers four access in Java, using different access modifier modified, the modified content will have different access rights,
public: public.
protected: Protected
default: default
private: private 

The concept of class will explain in the next article, modifiers for permission in advance to find out.

About two methods commonly used concepts, methods and overloaded method overrides

Method overloading: refers to the same class, the method allows the same name exists more than one as long as they can be different from the parameter list, and return the modifier
Return value regardless of the type.
Different parameter list: different numbers, different data types, different order.
 
Method override: refers to the subclass redefine methods of the parent class or an interface, the same parameters, the same return type, generally with a method of rewriting @Override annotations marked.

Guess you like

Origin www.cnblogs.com/zxxfz/p/10935545.html