Some simple knowledge points of object-oriented

1. The relationship between classes and objects

A class is an abstraction of an object, and an object is an instance of a class. Classes are abstract and do not take up memory, while objects are concrete and take up memory space. A class is a collection of objects with the same properties and methods, objects of the class can be created to instantiate objects.

2. The difference and connection between process-oriented and object-oriented

Both are programming ideas generated in the process of programming. The existing process-oriented, only object-oriented.

Process-oriented is mainly to develop and write methods or functions with specific functions, and there are different methods for each step of program operation, while object-oriented is to control the macro as a whole, abstract specific classes, attributes and methods, and then pass Objects are instantiated to satisfy methods or functions of the program. One is to focus on steps, and the other is to control the whole and assign tasks. Among them, object-oriented is more used in large-scale projects, because the requirements are relatively complex and the amount of engineering is large.

Both have encapsulation, process-oriented is to encapsulate methods or functions with specific functions, and object-oriented is to encapsulate classes.

Process-oriented has no inheritance and polymorphism, and the scalability and code reuse are poor; object-oriented has inheritance and polymorphism, which is more flexible in scalability and code reuse, and can better adapt to different environments .

3. The difference between method overloading and method overriding (overriding)

Method overloading, overload, requires that it must be in the same class, has nothing to do with the permission modifier, has nothing to do with the return value type, the method name is the same, the parameter list is different, and it has nothing to do with the exception, it is to provide a behavior in a class multiple implementations;

Method override or method override, override, requires that in the class with inheritance relationship, the method permission modifier of the subclass must be greater than or equal to the parent class, the method return value type must be less than or equal to the parent class, the method name is the same, the parameters The list is the same, the exception of the subclass should be less than or equal to the parent class, because the method in the parent class cannot meet the requirements of the subclass, the method of the same name is rewritten in the subclass, and the subclass object calls the subclass rewrite Overridden method (the method of the parent class is overridden by the method of the overridden subclass, there is also a saying that it is the principle of proximity).

4. The role of this and super keywords

this: represents the current object and is a reference to the memory address of the current object. similar to pointer

You can call the method of this class; you can call the constructor of this class, this() or this (parameter), which must be placed in the first sentence of the method; when ambiguity occurs, use this to represent member variables

super: represents the immediate parent class of the current object, just a keyword

You can call the method of the direct parent class, super.method name(); you can call the constructor of the parent class, super() (you can omit the default call when calling the no-parameter constructor) or super (parameter), which must be placed in The first sentence; non-private properties of the direct parent class can be called

Tips: This and super cannot call the constructor at the same time, because both of them are placed in the first sentence, which will cause the program to compile errors without knowing which one to call.

5. The role of the static keyword is divided into four parts: modified variables, methods, code blocks and inner classes

A statically modified variable is a static variable that is shared by the entire class, has only one copy in memory, and is initialized only once when the class is first loaded

The static-modified method is a static method, which can be called directly without relying on the object, but cannot be called by the this. method. Non-static variables and non-static methods of a class cannot be accessed in static methods, but static variables and static methods can be called in non-static methods.

Static modified code blocks are static code blocks, which are generally used to optimize program performance. Static code blocks can be placed anywhere in a class, or there can be an infinite number of them. They are initialized in order when the class is first loaded, and will only be loaded once.

A statically modified inner class is a static inner class, which belongs to the entire outer class rather than an object of the outer class. Non-static methods or variables of the outer class cannot be accessed, but static methods or variables of the outer class can be accessed.

6. Constructor calling rules under inheritance conditions

1. If the constructor of the subclass does not explicitly call the parameterized constructor of the parent class through super, nor does it explicitly call other constructors of its own through this, the system will call the parameterless constructor of the parent class first by default. In this case, writing or not writing the "super();" statement has the same effect.

2. If the constructor of the subclass explicitly calls the parameterized constructor of the parent class through super, the corresponding constructor of the parent class will be executed instead of the parameterless constructor of the parent class.

3. If the constructor of the subclass explicitly calls other constructors of itself through this, the above two rules are applied in the corresponding constructor.

4. Special attention is that if there is a multi-level inheritance relationship, when creating a subclass object, the above rules will be applied to the higher-level parent class multiple times until the no-parameter constructor of the top-level parent class Object class is executed. .

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325171205&siteId=291194637