- The composition of the class:
- Property (member variable, Field).
- Method (Member method, function, method).
Properties : member variables VS local variables
Same point: 1. Follow the format of the variable declaration-data type variable name = Initialize value
2. There are scopes
Different points: 1. The location of the declaration is different: the member variable is declared in the class, the method is outside the local variable: declared within the method, the formal parameter part of the method, or the code block.
2. There are four modifiers for member variables: Public private protected default. Local variables have no modifiers, same as the method modifiers in which they are located.
3. Initialization value: There must be an initialization value
Member variables: If the assignment is not displayed at the time of declaration, then different data types will have different default initialization values (byte short int long initialization value is 0,float double initialization value is 0.0)
(Char initialization value is a space, Boolean is false, the reference data type Initializes a value of NULL).
Local variables: Assign values that must be displayed. (Local variables do not have a default initialization value).
4. The two locations in memory are different: the member variable is stored in the heap space, and the local variable is stored in the stack space.
Method: provide an implementation of a function
1. Example public void Eat () {//Method body}
Public String GetName () {}
public void SetName (String n) {}
Format: Permission modifier return value type (void: no return value/specific return value) method name (formal parameter) {method body}
2. About return value type: void: Indicates that this method does not require a return value
A method that has a return value: At the end of the method there must be a variable of return+ return value type
3. Other methods or properties in this class can be called within a method, but cannot be defined within a Method!
- The principle of object-oriented programming in thought
1. Design and create components of class and class
2. Instantiating an object of a class
3. Complete a function in the form "object. Properties" or "object. Method"
Java Classes and objects