how2j.cn study notes

appreciated that the basic concepts of object-oriented java

This class is designed to hero

I believe we have played the king of pesticides or other games LOL, Now suppose we want to design such a game, the use of object-oriented thinking how to do it?

King pesticides there are many heroes, such as Angela, Daji, Ake, etc. All of these heroes have a common feature of the state, such as they have a name, blood, armor, speed and so on.

So we designed a thing called class representatives hero this type of thing.

Class: Hero (Hero)

Status characteristics: name, blood, armor, moving speed

public class Hero {
     
    String name; //姓名
     
    float hp; //血量
     
    float armor; //护甲
     
    int moveSpeed; //移动速度
}
复制代码

This example uses three kinds of data types are String (String), float (floating point), int (integer), this chapter only simple to use, will not start, explain in detail about the variable knowledge. This class is not the main method, do not try to run it. Not all classes are the main method.

Create a specific hero

A class is like a template, based on such a template, you can create a more specific one particular hero of heroes, one object called new Hero () is to create a hero java objects mean

public class Hero {
     
    String name; //姓名
     
    float hp; //血量
     
    float armor; //护甲
     
    int moveSpeed; //移动速度
     
    public static void main(String[] args) {
        Hero garen =  new Hero();
        garen.name = "盖伦";
        garen.hp = 616.28f;
        garen.armor = 27.536f;
        garen.moveSpeed = 350;
         
        Hero teemo =  new Hero();
        teemo.name = "提莫";
        teemo.hp = 383f;
        teemo.armor = 14f;
        teemo.moveSpeed = 330;
    }  
     
}
复制代码

Reproduced in: https: //juejin.im/post/5d0220ac51882518e845c90c

Guess you like

Origin blog.csdn.net/weixin_33933118/article/details/93177648