Java Advanced Features (1)--Understanding Object-Oriented Ideas

Foreword:

Excellent code has: high performance, reusable, extensible, easy to maintain, easy to understand

     Implementation:

             High performance: rational use of algorithms, data structures, etc.

             Reusability: encapsulation, inheritance

             Extensible: Polymorphism

             Easy to maintain and understand: naming conventions + annotations

    

         

 

 

 

 

 

Object-oriented is a kind of idea, which is based on process-oriented, that is to say, object-oriented is to realize functions through objects, encapsulate functions into objects, and let objects realize specific details; this kind of idea

It is to take the data as the first, and the method or algorithm as the second. This is an optimization of the data, which is more convenient to operate and simplifies the process.

 

Object-oriented has three characteristics: encapsulation, inheritance, polymorphism.

Among them, encapsulation refers to hiding the attributes and implementation details of objects, and only providing public access methods to the outside world, which isolates specific changes, facilitates use, and improves reusability and security. 

For inheritance, there is a certain relationship between two things, then the inherited class can obtain some properties and methods from the inherited class; this improves the reusability of the code. Inheritance is a prerequisite for polymorphism .

Polymorphism means that the reference of the parent class or interface points to the subclass object, which improves the extensibility of the program, that is to say, as long as the same interface or class is implemented or inherited, then the same interface or class in the parent class can be used.

The corresponding method to improve program extensibility, but the disadvantage of polymorphism is that the parent class reference cannot access the members in the subclass.

For example: that is: For example, if you are going to eat at a restaurant, you only need the restaurant, find the waiter at the restaurant, tell her what you want to eat, and then cook it for you, you don't need to know the meal What is wrong, you only need to face the waiter and tell him what you want to eat, and then he only needs to face you and receive the money after eating, without knowing how you eat the meal.

1. Features:

1: Simplify complex things.

2: Object-oriented turns the executor in the previous process into a conductor.

3: The idea of ​​object-oriented is a kind of thinking that is in line with people's thinking habits.

 

2. The three major characteristics of object-oriented: encapsulation, inheritance, polymorphism

1. Encapsulation: Only hide the properties and implementation details of the object, and only provide public access to the outside world

Benefits: Isolation of changes, ease of use, improved reusability, improved security

Principle: hide content that does not need to be provided externally; hide properties and provide public methods to access them

2. Inheritance: Improve code reuse; inheritance is the premise of polymorphism

Note:

①All constructors in the subclass will access the empty parameter constructor in the parent class by default. By default, the first line has super(); if there is no empty parameter constructor, it needs to be specified in the subclass; in addition, in the subclass constructor You can use this to specify other constructors of your own.

3. Polymorphism: a reference variable defined by a parent class or interface can point to an instance object of a subclass or a specific implementation class

Benefit: Improved program scalability

Disadvantages: When the parent class reference points to the subclass object, although the scalability is improved, it can only access the methods in the parent class and cannot access the methods in the subclass; that is, the limitation of access.

Premise: implementation or inheritance relationship; overriding the parent class method.

 

Guess you like

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