Overview class
- Package (encapsulation): Also known as data hiding. Formally, it is the combination of data and behavior, and the user of the object hides the way the data is implemented. encapsulation gives the object black box features for improved reusability and reliability .
- inheritance : extends the existing class to make it easy to define a new class. Subclasses have all the properties and methods of the parent class.
Object
- Three properties of the object:
- Object behavior: Actions that can be applied to an object
- State of the object: each object holds information describing the current feature, which may change due to a method call.
- Object identity: A unique way to identify an object (identity).
Eg
- An example of a destructive encapsulation
-
Class employee{ private Date hireday; Public Date Gethireday () { return hireday; } .... }
In the above example, the reference to date is returned, then date d = Gethireday () is called in the outer layer, and the change to D is actually a change to the hireday of the private variable. Therefore, to return a reference to a Mutable object, you should first clone it.
Static domain and static method static constants
Static methods
- is a method that cannot be manipulated on an object.
Factory method
Class Design Tips
- Be sure to keep your data private-------Never destroy encapsulation .
- Be sure to initialize the data, rather than relying on the system default values.
- Do not use too many basic types in a class, that is, use other classes instead of multiple related basic types.
- Decompose classes with too much responsibility.
- The class name and method name should be able to reflect their responsibilities.
<Java>< Classes and Objects ><OOP>