Home >Java >javaTutorial >Summary of knowledge points in java objects
What this article brings to you is a summary of knowledge points about Java objects. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Java is an object-oriented language, so it is necessary to record the various definitions and understandings of objects separately.
Facing objects, mainly including: object-oriented thinking, classes and objects and their use, memory map of objects, the difference between member variables and local variables, anonymous objects, encapsulation (private) , this keyword, constructor method, static keyword.
1. Overview: It is based on process-oriented thinking and is an encapsulation of process-oriented programming.
2. Object-oriented development: It means constantly creating objects, using objects, and directing objects to do things.
3. Object-oriented design: In fact, it is managing and maintaining the relationship between objects.
4. Object-oriented features: encapsulation, inheritance, polymorphism
5. The relationship between classes and objects:
(1) Class: a set of related attributes and Collection of behaviors
(2) Object: It is the specific embodiment of this type of thing
6. Anonymous object:
(1) It is an object without a name, which is a simplification of the object. Representation
(2) Two usage situations of anonymous objects: 1. When the object calls the method only once 2. Passing as actual parameters Relevant overview of the
1. Student s = new Student();What does it do in memory?
(1) Load the Student.class file into memory
(2) Create space for s in the stack space
(3) Create space for the student object in the heap memory
(4) Right The member variables of the student object are initialized by default
(5) Display initialization of the member variables of the student object
(6) Assign values to the member variables of the student object through the construction method
(7) The student object is initialized. Assign the object address to the s variable
2. Classes basically include: inheritance, polymorphism, abstract classes, interfaces, packages and guide packages, permission modifiers, internal classes
1. Overview: When the same attributes and behaviors exist in multiple classes, extract these contents into a single class. Then, multiple classes do not need to define these attributes and behaviors, and only need to inherit this class.
2. Class-to-class inheritance can be realized through the extends keyword.
3. This single class is called the parent class or base class or super class. These multiple classes can be called Subclass or derived class
4. With inheritance, when we define a class, we can define our own new members based on an existing class
5. Benefits :
(1) Improve the reusability of the code
(2) Improve the maintainability of the code
(3) Create a relationship between classes, which is the premise of polymorphism
(4) One drawback of classes is that the coupling of classes is very strong
6. Notes:
(1) Subclasses can only inherit all non-private members (members) of the parent class Methods and member variables)
(2) Subclasses cannot inherit the construction method of the parent class, but they can access the construction method of the parent class through the super keyword
(3) Do not inherit some functions of the class
7. The relationship between constructors in inheritance:
(1) All constructors in the subclass will default to the constructor with empty parameters of the parent class
(2) Because the subclass will inherit the parent class The data of the class may also use the data of the parent class. Therefore, before initializing the subclass, you must first complete the initialization of the parent class data
(3) The first statement of each constructor method is by default: super()
(4) If there is no constructor method in the parent class What to do:
[1] The subclass uses super to explicitly call other constructors of the parent class with parameters
[2] The subclass calls other constructors of this class through this
[3] It must be Note that super() and this() must appear in the first statement, otherwise, there will be multiple initializations of the parent class data
8. Method rewriting:
( 1) Overview: A method statement that is exactly the same as the parent class appears in the subclass, also known as method override, method overriding
(2) Private methods in the parent class cannot be overridden
(3 ) When a subclass rewrites a parent class method, the access permission cannot be lower
(4) The parent class static method, the subclass must also rewrite it through a static method
1, static keyword:
(1) Member variables and member methods can be modified
(2) Features:
[1] Loaded as the class is loaded
[2] Priority exists before the object
[3] Shared by all objects of the class
[4] Can be called through the class name
(3) Notes:
[1] There is no this keyword in static methods
[2] Static method Only static member variables and static member methods can be accessed
(4) The difference between static variables and member variables:
[1] Static variables belong to classes, so they also become class variables; member variables belong to objects, so they also Called instance variables
【2】Static variables are stored in the static area of the method area, and member variables are stored in the heap memory
【3】Static variables are loaded as the class is loaded, and disappear as the class disappears ;Member variables exist with the creation of the object and disappear with the disappearance of the object
[4] Static variables can be called through the class name or the object; member variables can only be called through the object name
2. Super keyword:
(1) The usage of super and this keyword are very similar. This represents the reference corresponding to this class, and super represents the identification of the parent class’s storage space (can be understood as the parent class’s Reference)
(2) Use super to refer to the components of the parent class, and use this to refer to the current object
3. Final keyword:
(1) The final keyword is final Meaning, you can modify classes, member variables, and member methods
(2) Modify classes, classes cannot be inherited
(3) Modify variables, and the variables become constants and can only be assigned once
(4 ) Modification method, the method cannot be overridden
1. Overview: A certain thing shows different states at different times
2. Polymorphic Premise and manifestation: There is an inheritance relationship, there are method rewrites, and there are parent class references pointing directly to subclass objects
3. Benefits:
(1) Improve the maintainability of the program (guaranteed by inheritance )
(2) Improve the scalability of the program (guaranteed by polymorphism)
4. Disadvantages: Unable to access subclass-specific functions
1. Overview: A method without a method body should be defined as an abstract method, and if there is an abstract method in a class, the class must be defined as an abstract class
2. Features:
(1) Abstract class And abstract methods must be modified by the abstract keyword
(2) Abstract classes do not necessarily have abstract methods, and classes with abstract methods must be abstract classes
(3) Abstract classes cannot be instantiated
(4) So, how is an abstract class instantiated? According to the polymorphic method, concrete subclasses are instantiated
(5) Subclasses of abstract classes, either abstract classes, or overriding all abstract methods in abstract classes
3. Abstract classes Member characteristics:
(1) Member variables can be variables or constants
(2) There are constructors, but they cannot be instantiated. So what is the role of the constructor method? Initialization for user subclasses to access parent class data
(3) There can be abstract methods to limit subclasses to complete certain actions
(4) There can also be non-abstract methods to improve code reusability
( 5) The abstract method must be public or protected (because if it is private, it cannot be inherited by subclasses, and subclasses cannot implement the method). By default, it is public
1. Overview: English is called interface. In software engineering, interface generally refers to a method or function for others to call. It is an abstraction of behavior.
2. Interface characteristics:
(1) Use The keyword interface represents: interface interface name {}
(2) The class implementation interface is represented by implements: class class name implements interface name {}
(3) The interface cannot be instantiated, but it can be implemented in a polymorphic manner , instantiated by a specific subclass
(4) Subclass of the interface: either an abstract class, or overriding all abstract methods in the interface
3. Interface member characteristics:
(1) Member variables: can only be constants, the default modifiers are public, static, and final
(2) There is no constructor method, because the interface mainly extends the function and does not exist specifically
(3) Interface Member methods can only be abstract methods, and the default modifiers are public, abstract
1. Class to class: inheritance relationship, only Single inheritance is possible, but multi-level inheritance is possible
2. Classes and interfaces: implementation relationships, which can be implemented singly or in multiple ways. You can also inherit multiple interfaces from one class and implement multiple interfaces at the same time
3. Interfaces and interfaces: inheritance relationship, you can inherit single or multiple inheritance
1. The interface is the abstraction of actions, and the abstract class is the abstraction of the source.
2. The abstract class represents what the object is, and the interface represents what the object can do
3. Neither abstract classes nor interfaces can be instantiated. If they are to be instantiated, abstract class variables must point to subclass objects that implement all abstract methods, and interface variables must point to class objects that implement all interface methods.
4. Abstract classes must be inherited by subclasses, and interfaces must be implemented by classes.
5. Interfaces can only be used for method declarations, while abstract classes can be used for method declarations and method implementations.
6. Variables defined in the interface can only be public static constants, and variables in abstract classes are ordinary variables
7. The abstract methods in the abstract class must be implemented by the subclass. If the subclass cannot implement all the abstract methods of the parent class, then the subclass can only be an abstract class. In the same way, when an interface is implemented, if all interface methods cannot be implemented, then the class can only be an abstract class
8. Abstract methods can only be declared, not implemented. The interface is the result of design, and the abstract class is the result of reconstruction
9. Interfaces can inherit interfaces and multiple interfaces, but classes can only inherit one.
10. Abstract classes can have Specific methods and attributes, interfaces can only have abstract methods and immutable constants
11. When you focus on the essence of a thing, use abstract classes; when you focus on an operation, use interfaces.
1. Overview: It is actually a folder that classifies and manages classes
2. Definition: package package name
3. Notes:
(1)The package statement must be the first executable code of the program
(2)There can only be one package statement in a java file
(3)If there is no package, the default means no package name
4. Compilation and operation of classes with packages:
(1) Manual:
[1] javac compiles the current class file
[2] Manually create the folder corresponding to the package
[3] Place the compiled class file into the folder created in the previous step
[4] Execute through the java command, which needs to be executed with the name of the package: java testpackage.HelloWorld
(2) Automatic type:
[1] Execute with the -d parameter when compiling javac: javac -d .HelloWorld.java
[2] Executed through the java command, the same as the manual type
5. Guide package:
(1) Overview: For access between classes under different packages, we found that every time a class under different packages is used, a package needs to be added. The whole route is more troublesome. At this time, java provides the package import function
(2) Package import format: import package name. Class name
1. Overview: Define the class in other classes Internally, this class is called an internal class
2. Access characteristics:
(1) Internal classes can directly access members of external classes, including private
(2) External classes must If you want to be a member of an inner class, you must create an object
3. Internal class position:
(1) According to the different positions of the inner class defined in the class, it can be divided into two formats: member position (Member inner class), local position (local inner class)
(2) Member inner class:
[1] Common modifiers: private, static
[2] The member inner class that is statically modified only Can access static members of external classes
(3) Local internal classes:
[1] Can directly access members of external classes
[2] Can create internal class objects and call internal class methods through the objects. Using local internal class functions
[3] Local internal classes must be modified with final to access local face changing. Because local variables will disappear when the method call is completed, at this time, the local object does not disappear from the heap memory immediately, and that variable still needs to be used. In order to allow the data to continue to be used, it is modified with final, so that what is stored in the heap memory is actually a constant value.
(4) Anonymous inner class:
[1] It is a simplified way of writing inner class
[2] Format: new class name or interface name (){}
[3] Premise: There is a Class or interface, the class here can be a concrete ordinary class, or it can be an abstract class
[4] Essence: It is a subclass anonymous object that inherits the class or implements the interface
[5] It can be said , the anonymous inner class is actually a subclass anonymous object
The above is the detailed content of Summary of knowledge points in java objects. For more information, please follow other related articles on the PHP Chinese website!