The vast majority of Java Programmers don't have to worry about the specific implementation of objects, as long as the objects meet their needs.
About OOP
Oop vocabulary
1. encapsulation; encapsulation, sometimes called data hiding
2. instance field: data in an object is called a strength field.
3. Method: The process of manipulating data is called a method.
4. State: Status
Another principle of OOP makes it easy for users to customize Java classes. This is: classes can be created by extending another class. In fact, in Java, all classes are derived from a "supernatural universe superclass", which is an object,
Object: To use Oop, you must understand the three main features of the object.
1. behavior (object behavior) ----- What operations can be applied to an object, or what methods can be applied to an object
2. Object state ----- how the object corresponds when those methods are applied
3. Identity: how to distinguish objects with the same behavior and status
If the object state is changed without calling the method, the encapsulation is damaged. **************
Relationships between classes
The most common relationships are: dependency ------- aggregation ------- inheritance
If a class method is used to manipulate objects of another class, we can say that one class depends on another class.
Minimize the number of dependent classes as much as possible. In terms of software engineering, the coupling degree between classes is minimized.
Objects and object variables
To use an object, you must first construct the object and specify its initial state. Then, the method is applied to the object.
Object variable:
You can explicitly set the object variable to null, indicating that this object variable does not reference any objects currently.
It must be realized that an object does not actually contain an object, but only references an object.
In Java, the value of any object variable is a reference to an object stored in another place. The return value of the new operator is also a reference.
Variables are not automatically initialized to null, but must be initialized. Call New or set them to null.
The method for modifying attributes is called the modifier method. A method that only accesses attributes without modification is called the accesser method.
Static Method
Static methods cannot be used to perform operations on objects. For example, the POW method of the math class is a static method. Expression: Math. Pow (X, A); Calculate the power
. It does not use any math object during operation. In other words, there is no implicit parameter.
It can be considered that the static method does not have this parameter (in a non-static method, this parameter indicates the implicit parameter of this method .)
Because static methods cannot operate on objects, they cannot access instance domains in static methods. However, static methods can access static fields in their own classes.
When to use static methods:
1. When a method does not need to access the object state, all the required parameters are provided by explicit parameters.
2. When a method only needs the static domain of the callback class.
Parameter passing
//// ********** A method can modify the variable value of the passed reference, the variable value corresponding to the call with the passed value cannot be modified ***********/////////////////
In Java, the usage of method parameters is as follows:
1. A method cannot modify parameters of a basic data type (that is, numeric and boolean values)
2. A method can change the status of an object parameter.
3. A method cannot allow object parameters to reference a new object.
Design skills:
1. Data must be designed as private
2. Be sure to initialize the data
3. Do not use too many basic data types in the class.
4. Not all attributes require independent attribute accessors or attribute changers.
5. Define classes in standard format
6. Break down classes with excessive responsibilities
7. Class names and method names should reflect their responsibilities