Learning object-oriented first learning interstellar

Source: Internet
Author: User

I started playing with the red police. At that time, I usually only created one barracks and one Machinery Factory. Then I went crazy and waited for one engineer to build it slowly (I only played the computer ).
Later, I started to get familiar with Starcraft. because it didn't have a fixed interface, it could create various units. Instead, I had to select a building and then select troops in the lower right corner of the screen.

Later, I found that in interstellar, I had to select an item first, and then there was an operation that could be done in the lower right corner. After getting used to it, I felt that the design of interstellar was more reasonable, it is more in line with the world.

At that time, I learned the Java course and never quite understood what object-oriented programming meant. What is the difference between it and the process-oriented approach? Suddenly I thought of interstellar, isn't that a classic case of object-oriented programming?

Let's take a look at what Java and interstellar have in common.

The core concepts of object-oriented programming are classes and objects. in interstellar, a unit is a class, and an instance of this unit is an object,
For example, zealot is a class, and the zealot you create in the game is an object.

The class includes data members and member functions, a unit in interstellar space, and attributes and its methods. Take zealot as an example, his attributes include blood volume, attack power, armor, radiation, and so on. His methods include movement, attack, and hold, which are shown in the lower right corner.
These attributes are read-only and reflect the encapsulation of the class. You cannot directly modify the blood volume of zealot unless you use methods such as A, or give him blood.

Another important feature of the class is inheritance. in interstellar, many units share the same attributes, such as blood volume, armor, and Green poison. There are also many common methods, such as move and stop. Abstract these features to form a base class. Units with these features inherit this base class. Of course, this base class won't appear in the game.
Class can also be combined, such as aircraft carriers including the body and small aircraft, tank includes the base and fort.

Class also has polymorphism, for example, for function overloading, right-click at the same time, but it can be mobile, attack or repair, which depends on the unit in the point, such as calling a function with the same name, which one is determined by the parameter. There are also run-time polymorphism, such as the same method, different objects have different meanings to execute, such as the same as the key, some units are attack, but mm is the blood.

I'm not sure whether the Red Police wrote it in a process-oriented language, but his operation method is undoubtedly in line with the process-oriented idea. He didn't select a unit first and then perform the operation, instead, select the operation and then check the unit of the operation. This operation is equivalent to a global function with parameters in each unit. Obviously, this disconnects operations from data because operations do not belong to any class.

Summary: The Star Game embodies the essence of Object-oriented thinking. If you want to learn Java and other object-oriented languages, I suggest you learn how to play the star games first.

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.