Hero design mechanism from World of Warcraft to snoop the object-oriented ideology

xiaoxiao2021-03-06  133

Crazy Niu Shu is starting to play Warcraft from the summer of zero, and now I am still a old bird. Are you also a Warcraft fan? However, if you come back, play things can not be lost, I don't know if you are playing these games, is it just like me, such as software design ideas, etc. Oh, don't yell at me.

It's good to come to the middle school (deliberate coughing two)! I will understand the object, the key is the three basic concepts: packages, inheritance and polymorphism. I explain in accordance with the order.

What is the package? Very simple, when you write the code, have you had a variable to declare the experience of Private? You don't want the outside world to pollute it - this is package. In a class, its internal variable (you can also intuitively think that attributes) is hidden in the external world by default. If you have to manipulate a class, then tell him directly what you want to do - what is it done, this you have no right.

Page, when you play Warcraft, those attributes of the hero, such as health, magic values, attack power, etc., in fact, it is packaged in the class of each hero. Then why can we read these properties? That is because these classes provide our interface to us to read attributes. For example, if you want to read or set the health of the monthly priest, you are actually unknowingly experienced the following steps. You first said to the monthly priest: "Month female priest, I want to see your vitality, can you?" She said: "This, I always publish my current value in my state. On the column, you will look at it. As for how I published my value, you have no right to know. "Then you don't even have inch, you want to change her life directly from the status bar. She will be very annoyance: "But I don't offer such services! You have to let me drink the water of the moon well!" Oh ~~~ Water in the moon is another and life-hooked Method, you have to modify your vitality via this interface. Such an interface played the purpose of restraining the user to avoid data illegal contamination and achieve the original intention of the package.

The method of reading attributes above is also called "read-only method", and also "writes only methods", the same truth, I am too lazy. What needs to be said is that all internal properties have interfaces to the outside world. Remember, the design is always necessary to make it easier in its user, so if a class writer thinks that some of the data is not necessary to use this class, then If he didn't drink too much wine that day, he would not provide any corresponding interface to that data. In this way, this attribute is completely encapsulated.

Of course, no one will stop you from declaring of variables into public, but no one is recommended, so please confirm that it is necessary before this statement. You can think of it, if the moon's priest puts the right to control her vicious value, then what is the fairness of the game?

"Total money ..." Your hustlership is open, "the class is such a thing! Hide its data, and through the way it is open to achieve the interaction of the user. Warcraft is so many heroes, Each family, plus six neutral heroes, twenty-two heroes are twenty-two classes! "Then I will touch your head and say:" Yes, you can really think so .hmmm, There are four magicals, there are health, magic values, maximum attack power, minimum attack power, defense value, etc. Twenty two heroes, we can write slowly, no problem ... "

At this time, you suddenly pushed my hand: "Slow! Since there is so many public attributes and methods, you don't try to write a abstract hero directly?" I said You are mad thinking, because this is the launch of the inheritance. If you don't have a single Warcraft, if you look at it, you are also full of examples in life and machine world. If you don't use their derived inheritance, and the impulse is designed to design each class into a separate class, then how many lines of lines will you be repeated? What does it seem to be a bit horrible? So we have to inherit, because such a mechanism has greatly improved our development software efficiency. Let us pull the topic back to our previous hero abstraction. I guess you don't dare to imagine such a class being created into an instance. What is it? Oh, I can't imagine anyway. Because I tried to do this, I will always have a spine egg in my mind, and the red soft soft, the ghost knows what it will be like it, anyway, good.

Please stop thinking here ... 12 seconds ...

If you are smart enough, you will be asked me: "So, since it is a abstract base class that is unwilling to be implemented, how do this method to write this class? For example, useultiMateskill ..." Well, asked! Since we don't want the abstract class being created into an instance, then it is very simple, we don't have to write code, but we sign a unilateral agreement before this class - we call it "Mustoverride" agreement. The text of the agreement is written this: "Do you want to inherit me? No problem! However, you must fill your own implementation of this method. Otherwise, don't expect to inherit me!"

So each hero is in a hurry to show its magic. The evil hunter said: "I want to play the rogue, let me come to a rogue bigger!" The big mage snorted on the side: "You fierce, Laozi can't afford to hide. 俺 俺 转嘿嘿 嘿.. "Death knights don't want to show weakness:" I am the highest, I want others to live back! "This can make the old bidt of the old chopped:" This year is noble, and I will let me die. , It is fully automatic! "... so twenty-two heroes have their own magic - a method name, really true!

At this time, you asked the mouth of the mouth of the mouth and asked: "Oh, brother, this will not be polymorphic?"

I said nonsense kid haha. This article has been published on 9CBS (Open in a new window): http://dev.9cbs.net/develop/Article/46/46413.shtm

转载请注明原文地址:https://www.9cbs.com/read-99230.html

New Post(0)