Java is an object-oriented language, which itself has three object-oriented features-encapsulation, inheritance, polymorphism. When developing, we want to remember that attributes are used to store data. be accessed directly, prone to security risks. Therefore, the properties in the class are usually privatized and provide a public access method to the outside. This method generally has two, the canonical wording: for the attribute xx, you can use SETXX (), GETXX () to operate it.
The member variables of a class exist in heap memory, and exist as the object is produced, disappearing. A local variable exists in the stack memory and is released as the owning region runs and ends. (emphasis on understanding)
Ii. what does creating an object do in memory? Person p = new person ();
1. First load the Person.class file in the specified location on the hard drive into memory.
2. When the main method is executed, the space (stack-to-stack) of the main method is opened in the stack memory, and a variable p is assigned to the stack of the main method.
3. Open an entity space in heap memory and assign a memory header address value. New
4. Spatial allocation of attributes in the entity space and default initialization.
5. Display initialization of the properties in the space.
6. The construction code block of the entity is initialized.
7. Call the constructor that corresponds to the entity, and initialize the constructor function.
8. Assign the first address to the P,P variable to reference the entity. (pointing to the object)
Third, this and static detailed
This: Represents the object. is the reference to the object to which the function belongs. Which object calls the function where this is located, and this represents which object. is the reference to which object. When defining a feature, this is used to represent the object if it is used internally to the object that invokes the feature. This can also be used to construct calls between functions. Call format: This (actual parameter);
This object is followed by the. Member properties and member methods are called (General method);
The This object is followed by the () constructor that calls the corresponding parameter in this class.
Note: The constructor is called with this and must be defined in the first row of the constructor. Because constructors are used for initialization, the initialization action must be performed. Otherwise, the compilation fails.
Static: The keyword is a modifier. Used to decorate members (member variables and member functions).
Characteristics:
1. Object sharing that wants to implement common data in an object. This data can be used for static modification.
2. Members that are statically decorated can be called directly by the class name. In other words, static members are more of a calling method. The class name. Static mode.
3. Static loads as the class loads. and takes precedence over the existence of the object.
Disadvantages:
1. Some data are object-specific data that cannot be statically modified. Because of that, the unique data becomes the shared data of the object. There is a problem with the description of the thing. Therefore, when defining static, it is important to be clear whether the data is shared by the object.
2. Static methods can only access static members, and non-static members cannot be accessed.
Because the static method loads, it takes precedence over the existence of the object, so there is no way to access the members of the object.
3. You cannot use the This,super keyword in a static method.
This cannot be used because this represents an object, while static is possible without an object.
4. The main function is static.
When do you define static members?
There are two types of members:
1. member variables. (data sharing is static)
Whether the data for this member variable is the same for all objects;
If so, then the variable needs to be statically decorated because it is a shared data. If not, then say that this is the object's unique data to be stored in the object.
2. member functions. (defined as static when no unique data is called in the method)
How can I tell if a member function needs to be statically decorated?
As long as the reference is made, the unique data in the object is accessed within the function. If there is access to unique data, the method cannot be statically decorated. If no unique data has been accessed, then this method needs to be statically modified.
The static life cycle is very long. A static code block is a region of code that has a static keyword. defined in the class. Completes initialization of the class. A static block of code executes as the class is loaded and executes only once (new objects are executed only once). If the main function is in the same class, it takes precedence over the main function execution.
Explanation of the main function: Ensure that the class is running independently. Is the entrance to the program. Called by the JVM.
The order in which static blocks of code, construction blocks, and constructors exist at the same time:
Static code block--Constructs a code block--constructor.
Iv. further discussion of inheritance
Inheritance is a hierarchical model of a Nexus class and allows and encourages the reuse of classes, which provides a way to articulate commonalities. When a subclass object is generated, Java defaults to calling the parent class without a parameter construction method, and then executes the constructor to generate the object of the parent class. Next, the constructor of the subclass is called, and the object of the child class is generated. (To create an object of a subclass, you first need to generate the object of the parent class, and there is no subclass object without the parent class object.) )
If a subclass uses super () to explicitly invoke a constructor of the parent class, it will look for the constructor that corresponds to super () and will not look for the parent class's constructor without arguments. As with this, super must also be the first execution statement of the constructor method, and no other executable statement can be preceded.
When two methods form an overriding relationship, the run () method of the parent class can be called through Super.run () in the subclass method, where Super.run () does not have to be placed in the first line of the statement, so the parent class object is already constructed and the parent class's run () is called first method or the run () method that calls the subclass first is determined by the logic of the program.
Override of Method (override)
Overriding requirements: The subclass override method and the method return type of the overridden method of the parent class, the method name, the parameter list must be the same, the subclass override method must be greater than the access permission for the method of the parent class, the method overlay can only exist between the child class and the parent class;
Method overrides the relationship between method overloads: Overloading two or more methods that occur inside the same class. Overrides occur between the parent class and the child class.
Final keyword use in inheritance, final can be used in the following four places:
1. Define variables, including static and non-static.
If the final decoration is a basic type, it means that the value given by the variable is immutable, that is, it is a constant, and if the final decoration is an object, it means that the reference to which the variable is given is immutable, and the immutable reference to the variable is not the object that the reference refers to. In fact, it is more appropriate to describe the final meaning of the description, that is, if a variable or method parameter is final modified, it means that it can only be assigned once, but the Java virtual machine to set the default value of the variable is not recorded once assignment.
The final modified variable must be initialized. There are several ways to initialize it:
1. Initialize at the time of definition
2. Initialize in initialization block
3. Initialize in the constructor of the class
4. Static variables can also be initialized in static initialization blocks.
1> defines the method.
When final is used to define a method, it indicates that the method cannot be overridden by the quilt class, but it does not affect its quilt class inheritance.
2> Defining classes
Since the final class is not allowed to be inherited, the compiler treats all of its methods as final when processing, so the final analogy is more efficient than the ordinary class.
Some notes on inheritance:
A. There is a parent class, and subclasses have
B. The parent class does not have a subclass that can increase
C. Parent class Yes, subclasses can change
D. Construction methods cannot be inherited
E. Methods and properties can be inherited
F. Method of constructing a subclass implicitly calling a parent class without parameters
G. When the parent class does not have a constructor method with no arguments, the subclass needs to use super to explicitly call the parent class's constructor, and super refers to a reference to the parent class.
The H.super keyword must be the first line statement in the constructor method.
V. Polymorphism
Polymorphism means that a reference to a parent class can point to an object of a subclass. In a class, you can define multiple methods with the same name, as long as you determine the number and type of arguments they have, which is called polymorphism of the class. The polymorphism of the class is embodied in two aspects: one is the overloading of the method, including the member method and the overloading of the construction method, and the other is the override of the method in the inheritance process.
VI. Abstract
Classes decorated with the abstract keyword are called abstract classes. Abstract methods need to be defined in an abstract class.
Seven, interface
The Java language does not support a class with multiple direct parent classes, but in real-world cases there are many examples of multiple inheritance. Java can not be multiple inheritance, but can implement (implements) multiple interfaces, the implementation of multiple inheritance indirectly.
Viii. comparison of abstract classes and interfaces
Same point:
1. Represents the abstraction layer of the system, when a system uses a class on an inheritance tree, it should try to declare the reference variable as the upper-level abstract type of the inheritance tree, which can improve the coupling between the two systems.
2. Neither can be instantiated
3. All contain abstract methods, which are used to describe what services the system can provide, but do not provide specific implementations.
Different points:
1. In an abstract class, you can provide a default implementation for some methods to avoid repeating them in subclasses, which is an advantage of an abstract class, but this advantage restricts multiple inheritance, and the interface can only contain abstract methods. Because of the inclusion of concrete methods in abstract classes, the ability to extend abstract classes to add concrete methods to abstract classes , does not affect its subclasses, and for interfaces, once the interface is advertised, it must be very stable, because the arbitrary addition of abstract methods in the interface affects all implementations, which either implement the new abstract method or declare it as an abstract class.
2. A class can inherit only a direct parent class, which may be an abstract class, but a class may implement multiple interfaces, which is an advantage of the interface, but this advantage is at the expense of not allowing the implementation of any method. When a subclass overrides an instance method of a parent class or hides a member variable and a static method of a parent class, the Java virtual machine takes on a different binding rule, which complicates the binding rule if it also allows a class to have multiple direct parent classes. Therefore, in order to simplify the system structure design and the dynamic binding mechanism, the Java language prohibits multiple inheritance. A class immediately has multiple interfaces and does not increase the complexity of the dynamic binding of the Java Virtual machine, because the Java virtual machine never binds the method to the interface, but only binds the method to its implementation class, using the general principles of the interface and the abstract class.
What happens in memory when Java creates an object