Object Oriented (i)
Property
member variables and local variables:
Same point:
- Format of the loop variable declaration: Data type variable name = initialization value
2. There are scopes
Different points:
- Declaration location is different: member variable declared in class, outside method
A local variable is declared within a method, a parameter part of a method, within a code block
The difference between member variables (written in class--class) and local variables (written in method--main)
Initialization of member variables (there is a picture of the truth)
Member variables can be any of the data types in Java (underlying data types and reference types)
Member variables can be initialized or uninitialized (Java default initialization)
Member variables are scoped to the entire class
2. The modifier for the member variable has four: public protected default private permission is decremented from large to small
Local variable has no modifiers, same as the method modifier in which it is located
3. Member variable exists in heap space, local variable exists in stack space
Summary: Classification of variables: (1) by data type: base data type (8) & Reference data type
(2) According to the location of the declaration of the different: member variables & local Variables
Method
The implementation of a function is also a method, and the property inside the main method must also be assigned a value.
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) {}
2 about return value type: void: Indicates that this method does not require a return value
There are methods for returning values: At the end of the method there must be a return + variable corresponding to the return value type
Memory: Void and return can not appear in a method at the same time.
Other methods or properties of this class can be called within the 3 method, but methods cannot be defined within a method
Structure of the memory partition:
Stacks: Local variables, reference names for objects, reference names for arrays
Heap: New "things" (e.g. entities of objects, entities of an array), with member variables
Method Area: Contains string constants
Static domain: A variable declared as static
overloading/overriding of methods
Overloading of methods (overload)
Requirements: 1. In the same class 2. The method name must be the same 3. The parameter list of the method is different (the number of ① parameters differs from the ② parameter type) the permission modifiers of the method can be different
Add: The overloads of a method have no relation to the return value type of the method
The following four methods form an overload
Defines the and of two variables of type int
public int Getsum (int i,int j) {
return i + j;
}
Defines the and of three variables of type int
public int getsum (int i,int j,int k) {
return i + j + k;
}
Defines the and of two double type data
Public double getsum (double d1,double D2) {
return D1 + d2;
}
Defines the and of three array of type double
public void Getsum (double d1,double d2,double D3) {
System.out.println (D1 + d2 + d3);
}
cannot be overloaded with several methods as above
public int getSum1 (int i,int j,int k) {
return i + j + k;
}
public void getsum (int i,int j,int k) {
System.out.println (i + j + k);
}
The following two methods form an overload.
public void method1 (int i,string str) {
}
public void Method1 (String str1,int j) {
}
Java Object-oriented