Non-parametric method of class
What are the parts of the method of the class?
Definition of the method:
1. Access permission Modifier 2. The data type returned by the method 3. Name of the method 4. Body of method
What are the differences between member variables and local variables?
~ Scopes are different
-The scope of the member variable is visible within the entire class
-The scope of a local variable is limited to the method that defines it
~ Initial values are different
-Java will give the member variable an initial value
-Java does not give initial values to local variables
~ in the same method, local variables with the same name are not allowed;
In different methods, you can have local variables of the same name
-Local variables have higher precedence when two types of variables have the same name
A method of a class defines some behavior (or function) of a class
syntax:
defines a method for a class:
//Step 1: Define method Name and return value type
Public return value type party Dharma name () {
//Step 2: Write method body
//The return value of the principal
}
Method for writing methods here
Two cases:
If the method has a return Back value, the method must return the value using the keyword return, which returns the type of the return value of the value type.
Syntax: return expression: function: Jump out of method, return result
If the method does not return a value, the return value type is: void
Method call
method is a "black box" to complete a particular application function and returns the result
method call: The statement contained in the execution method
Syntax: Object name. Method name ();
Method Invocation Summary:
methods allow mutual invocation, do not need to know the specific implementation of the method, implement reuse, improve efficiency
Common errors:
1. The return value type of the method is: void, and the method cannot have return value!
2. The method cannot return multiple values. Can only be a single value!
3. Multiple methods cannot be nested with each other definition!
4. Cannot write program logic code directly outside the method!
Note:
The difference between a member variable and a local variable
Different scopes
The scope of a local variable is limited to the method that defines it
The scope of a member variable is visible within the entire class
Different initial values
Java will give the member variable an initial value
Java does not give initial values to local variables
Local variables with the same name are not allowed in the same method
In different methods, you can have local variables of the same name
In the same class, a local variable has a higher precedence when the member variable has the same name as a local variable.
Common errors:
The scope of the local variable AVG is limited to the Calcavg () method
Summarize:
The method that defines the class must include the following three parts
The name of the method
Type of method return value
The body of the method
The method invocation of the class, using the following two forms
Methods in the same class, called directly using the method name ()
Different classes of methods, first create the object, and then use the
"Object name. Method Name ()" To invoke the
In Java, there are member variables and local variables, and their scopes are different
The Java doc comment starts with "/**" and ends with */!
Java Chapter 11th