The fourth chapter object and class
OOP three features: behavior, State, identity of an object
Relationships between classes: dependencies, aggregations, inheritance
Dependency: The method of one class manipulates an object of another class. (Minimizing this coupling)
Aggregation (HAS-A) A class exists as a variable of another class
Inheritance (IS-A)
If you declare only one class variable, and you do not open it in the heap space,
Then this variable cannot invoke the method in the class
Because this variable does not reference the object
!! A variable of an object does not actually contain an object, but simply refers to an object
Data data;
Equivalent to:
Data *data; It represents only one pointer, not the object data
Get/set accessors
There is only one public class in a source file
Javac <==> make compiles the class containing the main method, and the runtime automatically compiles other related classes
You can also use a wildcard character *
C + + methods defined inside a class are inline methods
In Java, virtual-opportunity automatic monitoring calls are often called, optimized methods
Do not write accessor methods that return references to mutable objects, and if you want to use them, you should return a clone copy
Class Employee
{
。。。
Private Data gethireday ();
{
return Hireday.clone (); To prevent the mixing state when assigning values to other data variables
}
}
Final and Static
/////////////////////////////
Static
C: preserves local variable data. Data has local concealment
C + +: The variable belongs to the entire class and not to the specific object
/////////////////////////////
Main (); Test tip: Each class can have a main method, and you can run a class individually. Running the program can be Java application, and the main method in other non-startup class classes will not run
Java is a value pass
A method cannot modify a parameter of a base data type (numeric, Boolean)
A method can change the state of an object parameter
A method cannot have an object argument referencing a new object
Method names are combined with parameter lists to form method signatures
Java initialization object: Come back and look!!
Overloaded constructors
This invokes other constructors
No parameter constructor
Object Initialization block
Static initialization blocks
Instance domain initialization
Object destructor: Finalize does not recommend not knowing when to call
Package import with ingest package is similar to reference namespace
Package = = Namespace
Import = = using
Static Import
Class Design Tips
1) Guaranteed Data privacy
2) Initialize the data
3) Do not use the base type too much in the class (you can use the aggregation relationship instead)
4) Not all domains require separate accessors and converters
5) Decomposition of classes with excessive responsibilities
6) The class name and method name should be able to reflect their responsibilities
Java Learning notes (Core Java) 4 objects and classes