Java (2) to the object village, java to the object Village
This article describes the differences between object-oriented and process-oriented and the advantages of object-oriented.
Hear the voice of programmers who use process-oriented programming to write programs:What are the things this program has to do? What procedures do we need?
Aha, my idea as a qualified quasi-java programmer is:What are the things in this program... who are the key players?
The difference between forward Object and process orientation can be embodied in the image. When writing a program for a process, you are concerned about the sequence in which the program is executed, while the object-oriented program is concerned about what is in the program (the formal name is a class ).
For example, the project manager needs to give you a requirement to write a program that rotates and plays music (in mp3 format) in a graph (triangle, square direction, circle, when playing music, the artist's profile picture is rotating ).
As a senior C language player, I found out two important processes of this program with a little thought: Graphics rotation and music playing. So his program is:
Rotate (shapeNum ){
// Make the shape rotate 360 °
}
PlaySound (shapeNum ){
// Use shapeNum to lookup which
// AIF sound to play, and play it
}
I thought for a long time and found the class: graphics. (Haha)
What are the attributes of this Class Based on the object-oriented thinking? I did not mention it. Class behavior? It involves rotating and playing music. Well, write the program after analysis.
Class cirlce {
Ratate (){
// Code to ratate a circle
}
PlaySound (){
// Code to ratate a circle
}
}
Class Square {
Ratate (){
// Code to ratate a circle
}
PlaySound (){
// Code to ratate a circle
}
Now the project manager says the demand has changed
Among the three main features of object-oriented: inheritance and polymorphism, the code reuse rate has been improved.
Object-oriented has many advantages. You can try it yourself,In programming learning, it is very important to try it yourself.
A small suggestion:
If you’re stuck on an exercise, try talking about it out loud. Speaking (and hearing) activates a different part of your brain. Although it works best if you have another person to discuss it with, pets work too. That’s how our dog learned polymorphism.