method

Method: A block of code that accomplishes a specific function.
Format:
Modifier return value type method name (parameter type parameter name 1, parameter type parameter name 2...) {
                       function body;
                       return return value;
}
Modifier: public static
Return Type: Ambiguous Return Type: void, with or without a return statement.
     Explicit return value types: basic data types (four types and eight types), reference data types: classes, etc. . .
Return: End the method and return the return value to the caller.
Class: A collective term for things that have the same properties. Classes are abstract concepts, descriptions of things.
The heap memory stores what is new, and the stack stores local variables.
Private modifier: private. Protect the security of member variables and cannot be accessed casually. Variables modified by private can only be accessed within this class.
The difference between member variables and local variables:
1. The location in the class is different:
   Member variables: outside the method inside the class
   Local variables: inside a method  
2. Different locations in memory:
   member variable: heap memory
   Local variables: stack memory
3. The life cycle is different:
   Member variables: Appear as objects are created and disappear as they are collected by the garbage collector.
   Local variables: appear as methods are pushed onto the stack and disappear when they are popped off the stack.
4. Scope of action:
   member variable: the entire class
   Local variables: start where it is defined, and end at the code block where it is defined.
Define method:

Calling the method: After defining the method, it must be called, otherwise the method will not work


method call:
Direct call: sum(13,23); (meaningless), there is no method with a clear return value, only direct call can be used.
Output call: System.out.println(sum(12,34));
Assignment call: assign the return value of the method to a variable (the data type of the variable is consistent with the return value type) int sum = sum(45,67);
When calling a method, if the method has parameters, you need to pass in the actual parameters according to the parameter type and number of the method. The parameters correspond one by one in order, regardless of the parameter name. The parameter names of the actual parameter and the formal parameter can be the same. It doesn't matter.
private int px;//Pixel is private and cannot be accessed outside the class. If you want to assign a value to it, you can use the public set method. If you want to get the value, you can use the public get method.
public void setPx(int px){
        this.px = px;//When member variables and local variables have the same name, the principle of proximity is adopted, and the px to the left of the equal sign is also a local variable, so use the this keyword to indicate who calls this method on the current object, and this refers to who .
        System.out.println("Assignment"+px);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324708445&siteId=291194637