8.1 Access Modifiers:
Public: This class or non-class is accessible;
Private: Only the class can be accessed;
Protected: Members of the class and its subclasses can be accessed, and classes in the same package can be accessed;
Default: Classes in the same packet can be accessed.
The purpose of the access modifier is to limit the range of properties and methods that are used and called.
8.2 Package
Information is organized in accordance with the principle of information hiding. The package has two characteristics: the collection of information, the hiding of information.
8.3 accessor get () and modifier set ()
The difference between a private property +get/set () and a public property:
Private attribute +get/set (), you can make a read-only or write-only property by removing the Get or set method, but the public property cannot.
A private property can verify the validity of a property value by doing some validation before the set method sets the property value. However, the public attribute cannot be done.
8.4 Static blocks and instantiation blocks
public class x{
static{}
} is called a static block that executes when the class is loaded and executes only once.
Publick Class x{}
{} is called an instantiation block, and each time an object is generated, an instantiation block is executed, followed by super ().
When an object is instantiated and the class contains an instance initialization block, the following events occur sequentially:
- The corresponding constructor in the subclass is called;
- To perform a call to Super, the control flow jumps to the corresponding parent class constructor;
- After the parent class constructor executes, the control flow jumps back to the subclass constructor;
- The instance initialization block executes before any statements after super () in the subclass constructor are executed;
- Finally executes the statement after Super () in the subclass constructor
8.5 Internal Classes
In Java, you can also define classes inside a class. This class, defined inside a class, is called an inner class. The class in which the inner class resides is called an external class.
An inner class is equivalent to an attribute of an outer class, and an object of an inner class must depend on an object of the outer class.
The class file name generated by the internal classes is "external class name $ internal class name."
Java object-oriented programming--advanced concepts of the eighth class