abstraction, encapsulation, inheritance, polymorphism

abstract

Abstraction is the process of extracting common and essential features from many things and abandoning their non-essential features.

The following is the process of abstraction to encapsulation

If you don't consider the actual situation,
the definition of a person: has a mouth, feet, eyes, ears, can eat, can run.

Definition of animals: have mouth, feet, eyes, ears, can eat, can run, and have fur.

Therefore, monkeys and orangutans belong to both animals and humans, humans and animals (that is, what we call classes).

Encapsulation is a combination of common feature quantities according to certain rules.

Abstraction is the extraction of common feature quantities.

encapsulation

Encapsulation means combining the properties and methods of an object into an independent system unit, and hiding the internal details of the object as much as possible.

Encapsulation is the basis of the description of object-oriented ideas. From then on, programmers are no longer faced with many complex functions and process implementations, but very few individual instances with behavioral capabilities.

For example, for lovebirds and crows, when we are abstracting, we ignore those things such as mouths that are different, and we only care that they all have mouths: mouth; we ignore the difference in the color of their feathers, but only care that their feathers are colored: color; then we abstract the characteristics of the mouth and feather color .

Then we encapsulate these features into birds by abstracting them , which is encapsulation.

inherit

The subclass object has all the same attributes and methods as its base class, which is called inheritance.

Also take this picture to explain

Inheritance is because all the features of the parent class conform to the subclass, and the subclass contains features that the parent class does not have, so the subclass itself retains the features that the parent class does not have, and the features common to the parent class are obtained by inheriting the parent class.

like:

Because monkeys have tails, but animals do not contain tails, and humans do not contain tails and hairs. If you want to express monkeys at this time, you need to create a monkey class that retains the characteristics that animals or humans do not contain, and then inherit animals or humans;

Class Animate{

......

}

Class People {

......

}

Monkey expression:

 class Monkey extends Animate {

  Define tail feature quantity

}

or

 class Monkey extends People {

  Define tail feature quantity

  define gross features

}

polymorphism

Polymorphism means that after the attributes and behaviors defined in the base class are inherited by subclasses, they can have different characteristics such as data types or performance behaviors.

For example, monkeys and orangutans both inherit the animal class but run in different ways, so the implementation of the running method is naturally different, and the subclass root realizes the feature quantity in its own free and unique way.

Guess you like

Origin blog.csdn.net/sunboylife/article/details/130603058