Tips: This article tries to explain object-oriented thinking in plain English.

HuangGu period

God first created several original classes. For example, Animal, Plant, Topography, and Atmosphere are the beginning of everything on Earth, and in this paper we will use animals as examples.

Classification,

After creating Animal, add common attributes to Animal: mouth:true and common methods eat(){},mating(){}.

And then he decided to start subdividing, and we made it a little bit simpler, not by “phylum compendium”, but by Human, Cat, Dog, and so on.

He created Human according to Animal. We can say that Human inherits from Animal, so there will be common attributes and common methods of Animal in Human. Similarly, humans have their own public attributes: Stand :true and public methods: farming(){}. Then, God created a man based on Human: Xiao Ming.

Xiao Ming is an instance object created based on Human, a 20-year-old male programmer who plays the piano. He inherits the public properties and methods of both Animal and Human classes

mouth:true,
stand:true,
farming(something){
    ...
},
eat(something){
    ...
},
mating(someone){
    return xxx
}
Copy the code

His private property is occupation:programmer, and his private method is playingPiano(){… Only when he tells others about himself will others know. Besides, they don’t know what he can play. They can only say, “Xiao Ming, please play the piano for me.” Call xiao Ming. PlayingPiano () [after encapsulation can only be used after providing external interface]

One day, Xiao Ming met a girl xiao Bai, who was 21 years old and could sing customer service. The private attribute was customService and the private method was singing(){… }. Later, they fell in love and got married. One day, Xiao Ming called his method “mating” and sent Bai as a parameter, and then returned a child, Xiao Hei. However, hei did not inherit xiao Ming’s playingPiano and xiao hong’s singing, instead, she had her own private attribute tellingStory. But the only thing that is the same as Xiao Ming and Xiao Hong is that he inherits the public attributes and methods of Animal and Human.

At the same time, another couple, xiaohua and xiaohua, also call their mating, but what they return is another child, xiaoyue, who is different from her parents in private attributes. She is playingBasketball, but the same. She also inherited the public attributes and methods of Animal and Human.

Why are the children who return from calling each other different? That’s polymorphism. When the same operation (method) works on different objects, it produces different execution results.

Above, the three basic characteristics of object – oriented encapsulation, inheritance, polymorphism are finished. Certainly not enough in-depth, if there is any understanding is not correct welcome in the comments section, also welcome everyone to discuss.