Java (2) to the object village, java to the object Village

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.