Overview of Java Object-Oriented OOP Thought

OOP (Object Oriented Programming)

Classes and objects

In object-oriented, classes and objects are the most basic and important components.
Class: A class can be said to be a template, which describes the behavior and state of a class of objects.
Object: An object is an instance of a class with corresponding state and
What is the behavior class for: simulating reality, encapsulating data and code.
If it is a common animal dog in life, it can also be said to be a class, then specific objects such as: Chinese pastoral dog, husky, pug, etc., each A dog can be said to be an object, with its corresponding characteristics (attributes) and behaviors (methods), characteristics such as the dog's color, name, etc., behaviors such as barking, swimming, eating, etc.

Generally speaking: a class is a collection of
objects that describe a group of objects with the same characteristics (attributes) and behaviors (methods). The characteristics of the objects are expressed as attributes of the class in the class, such as the color, name, type of the dog, etc. Huskies and pugs are both dogs, but they may have different names or colors
. The behavior of objects is a method of the class. For example, a dog has a behavior that can bark, so "how to bark" is the method of the class.
Here are some examples in reality. Examples to facilitate understanding of classes and objects

class Object
people Han Meimei who is taking an exam, Xiao Ming, a student in class, and Zhang San who is the champion
animal A dog in a fight, a sleeping cat, a fish in the water

interface

What is an interface?
The examples in the book java and patterns are very vivid: We can unplug the electric fan and plug in the charging plug of our computer for the commonly used sockets in the home. That is to say, these appliances are all pluggable constructions. They are pluggable because they all have plugs that match the sockets.
Then the so-called interface is actually equivalent to a socket. Pluggable construction is the electrical appliances we use. Interface is the key to realize the pluggability of construction.
What is the interface for?
1: Interaction specification between classes
Insert picture description here
2: Define the interface between function users and function providers
Why use interfaces?
1: Isolate changes
2: Interface is a guarantee of pluggability

Abstract class

What is the purpose of abstract class: Tolerate change and invariance.
Abstract class only provides part of the realization of a type. There can be abstract methods and concrete methods at the same time. Abstract methods can be said to be the part of change, and concrete methods are part of the invariance.

Three characteristics of OOP

Encapsulation

Encapsulation can be regarded as a protective barrier to prevent the code and data of this class from being randomly accessed by the code defined by the external class. To access the code and data of this class, interface control is required. Encapsulation can make the code easier to maintain and understand the
encapsulated Advantages:
1: Encapsulation can reduce coupling
2: The code inside the encapsulated class can be modified at will
3: Hide the implementation details, just call the interface directly

inherit

Inheritance is a cornerstone of Java object-oriented programming technology. Inheritance means that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance domain and methods of the parent class, or the subclass inherits methods from the parent class, so that the child The class has the same behavior as the parent class, or the subclass overrides the behavior of the parent class and re-implements its own behavior.
Inherited characteristics:
1: The subclass has non-private properties of the parent class, method
2: The subclass can have its own properties and methods , That is, the subclass can extend the parent class
3: The subclass can implement the method of the parent class in its own way
4: Java inheritance is single inheritance, but multiple inheritance is possible, single inheritance means that a subclass can only inherit one parent class , Multiple inheritance means, for example, class A inherits class B, class B inherits class C, so according to the relationship, class C is the parent class of class B, and class B is the parent class of class A.
5: Improve the coupling between classes

Polymorphism

Polymorphism is the same behavior with different manifestations, the same interface, different instance implementations may have different results
What does polymorphism provide us: a way to achieve change
The advantages of polymorphism:
1: eliminate the type of Coupling relationship between
2: replaceable
3: expandable
4: flexible and simplified

The form of OOP reuse

The relationship between class and class:
1: inheritance
Insert picture description here
2: holding
combination, class 2 holds an instance of
class 1 class 2 uses class 1
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_34365173/article/details/108012960