What is object-oriented programming (OOP)?

The first Java programmers to understand the basic concept is this: What is object-oriented programming (OOP) ?

Played DOTA2 (a tower push to kill the game) do? Monkey King, which has a role in the European team playing very slip, but not how the domestic team will play, will not apply on their own to play the hero myth, it is a bit sad.

We define a class in Java Monkey King Bar:

public class SunWukong {
    private Integer healthPoint;// 生命值
    private Integer magicPoint;// 魔法值
    
    /**
     * 棒击大地
     */
    private void rodHittingTheEarth() {
        
    }
    
    /**
     * 丛林之舞
     */
    private void jungleDance() {
        
    }

}

We gave him two attributes (health and mana healthPoint magicPoint), give him two skills (clubbed to earth rodHittingTheEarth()and jungle dance jungleDance()).

If I were LGD (old godfather, well-known DOTA team) coach 357 (resigned), a highly intelligent good coach (really bald), is responsible before the BP (Ban and Pick, mix out the other choose one's own heroes and hero) work.

Now, TI9 the group stage (has ended), LGD against OB (European team, two-time champion) in the first inning.

I --357 in the first stage BAN BP lost more restrained Monkey King Beastmaster and winding technician, then in the selection stage (PICK) selected Crystal Maiden Ice Maiden to the Monkey King and the hairy guy with.

So, I chose the Monkey King at this stage, is to say that creates an object of the Monkey King, it is equivalent in Java new SunWukong(). Selected the Monkey King, who is going to play with? Nature is the team known as the world's first single maybe.

Object-oriented programming (OOP) is actually not difficult to understand, not that the English word Object-Oriented Programmingacronym thing. OOP gives us a concept that "Everything Is an Object" - we can use your imagination as much as possible, the reality of the matter (including animals, people, even non-living things) abstract as a computer language (such as Java) can understand the model, then the next, computers to solve real-world problems become simple again.

The power of OOP is that it saves us from having to simulate real-world objects in accordance with the limitations of the computer; we can real problems to be solved based on constructing objects, to make this object has real substance has characteristics (corresponding to Java class attributes) and behavior (corresponding to the Java class methods). Different objects, objects having properties and methods will be different.

Also take DOTA hero for it.

All heroes can be abstracted as a class (object is an instance of the class), this class contains hero attributes corresponding growth (value of life, mana, damage value, armor, etc.), and the corresponding skills behavior. So, how to differentiate each hero do? For example, Ice Maiden Crystal Maiden is a magic helper class hero, so some of her natural initialize the value of life is lower than the Monkey King agility classes, but mana higher; In addition, their skills are completely different - Monkey King holding a golden cudgel, so his skills have clubbed to the ground; crystal Maiden holding a wand, so her skills have Frost Nova, Frostbite and so on.

Object-oriented programming gives us some important lessons:

1) Everything object.

2) the object program is a process of mutual cooperation, to tell them what to do with each other by sending messages.

3) Each object has its own type, that is, " Each object is an instance of a class (abstract data type) ." While each object is unique (assigned a unique address in memory), but with the same state (attributes, variables) and behavior similar objects (methods) can be assigned to a class.

4) Each object provides some services. For example, you can jump on the tree Monkey (Jungle Dance), then jump down and deceleration caused damage to enemies in the area (universe jump).

Object-oriented programming code is easy to maintain and reuse, easily extensible, object-oriented because there are three characteristics: encapsulation, inheritance, polymorphism.

1) packages, each object template - like the freedom to define the properties and behavior, making it different from other classes.

2) inheritance, subclasses can easily reuse the code of the parent class (attributes and behavior), need only one key - extends. If the parent class's attributes do not meet the needs of the subclass, may be added; if the parent class does not satisfy the requirements subclass can override.

3) polymorphism, this characteristic is derived based on the foregoing two characteristics, some of the more difficult to master. You can first try to understand it. There are two objects, the object is a subclass of a parent class object, if time passes them to the same method, the parameters of the method may define the type of the parent class (if class is defined as a sub-type of when the parent will not be delivered), in the implementation of the method it will automatically recognize the transfer of the parent class or subclass.

Encapsulation, inheritance, polymorphism these three characteristics for beginners, big nod. But do not worry, I will continue to thoroughly described in the following article. Micro-channel search " silence the king, " public number, reply 'attention after the free video "500G Java obtain high-quality instructional videos.

Guess you like

Origin www.cnblogs.com/qing-gee/p/11774867.html