4th Chapter objects and Classes
1. Do not write accessor methods that return reference to mutable objects
For example:
Class Employee
{
private Date hireday;
Public Date Gethireday ()
{return
hireday
}
} Employee e = ...;
Date d = e.gethireday ();
At this point, D is the hireday of E, and changes to D directly result in Hireday changes to E.
2. If two objects belong to the same class, then one object's methods can access the private data of another object.
That is, the method of the employee class can access the private domain of any object of the employee class. For example:
Class Employee
{
...
public boolean equals [Employee other]
{return
name.equals (other.name);
}
}
Call: if (Harry.equals (Boss)) ...
At this point, not only was Harry's private domain visited, but also the boss's private domain was accessed.
3.private final Date hiredate only means that the object references in the HireDate variable cannot be changed after the object is constructed. Non-final data fields for HireDate can be changed.
4. A static method is a method that cannot be applied to an object and cannot access a static domain, but it can access a static domain.
For example: Math.pow (x,a);
You can use static methods in the following 3 situations:
(1) A method does not require access to the state of an object, and its required parameters are provided through explicit parameters.
(2) A method only needs to access the static domain of the class.
(3) Use the factory method to create different types of objects, like the NumberFormat class.
NumberFormat currencyformatter = Numberformat.getcurrencyinstance ();//static method
NumberFormat Percentformatter = Numberformat.getpercentinstance ();//static method
5. Each class can have a main method.
This is a technique for using a class for unit testing.
For example, the employee class has a main method, the application class is a larger class, and there is a main method. In this case, you want to test the employee class independently, just Java employee, and when you use Java application, you will only invoke the main method of application, not the employee's main method.
6. Implicit parameters and display parameters
For example, Number007.raisesalary (5), where number007 is an implicit argument, and 5 is the display parameter.
This can be used to represent an implicit parameter.
7. Method parameters.
Two kinds: Value calls and reference calls
The object reference in Java that is used in a function parameter is a value call, not a reference call. This means that the object parameter cannot be referenced to a new object.
Summary: (1) when the parameter of a method is a reference type, changes to the data field of the parameter directly cause the real object's data field to change.
(2)
public static void Swap (employee x, employee y)
{
Employee temp = x;
x = y;
y = temp;
}
Execute statement swap (EMPLOYEE1, employee2);
Note: The objects referenced by employee1 and Employee2 do not exchange at this time.
8. The system provides a default constructor with no parameters only when the class does not provide any constructors.
9. In a constructor, you can use this to invoke another constructor. for example,
Public Employee (double s)
{This
("Employee #" + NextID, s);
nextid++;
}
When the employee (double) constructor is invoked, the employee (String, double) constructor is also invoked.
10. Initialization Block
Three ways to initialize a data field: (1) Assign a value in a declaration (2) Assign a value in the constructor (3) Use the initialization block
Become the initialization block as follows:
Class Employee
{
private static int nextid;
private int id;
Initialization block
//As long as the object of the constructed class is initialized, the initialization block is executed
{
id = nextid;
nextid++;
}
Static initialization blocks (static initialization blocks are executed first)
static
{
Random generator = new Random ();
NextID = Generator.nextint (10000);
}
Constructor
...
}
Initializes the data field order: Executes the initialization block first (static), and then calls the constructor.
11. Package
(1) A class can use all the classes in the owning package, as well as the public classes in other packages.
(2) You cannot import all packages that are prefixed with Java, such as the import java.*; that's not going to work.
(3) The import statement can import not only packages and classes, but also static fields and static methods in a class. For example, import static java.lang.System.out;
(4) Compile command: Javac Com/mycompany/payrollapp.java
and interpreter command: Java com.mycompany.PayrollApp is using. Instead of/
(5) If the package name of a class is incorrectly written, the source file can be compiled correctly (if it is not dependent on other packages) but will not be able to execute because the virtual machine cannot find the. Class class file.
(6) Package scope. Variables that are marked public can be accessed by any class, and those that are marked private can only be used by their classes, and if they are not specified, they can be accessed by methods in the same package.
12.Java initializes only the data field and does not initialize the local data variables.