【LOL && DOTA】面向对象详解

【LOL && DOTA】面向对象详解

相信你肯定玩过DOTA或者LOL,没玩过,也一定听说过身边有很多的朋友在玩这款游戏的

假设,我们要设计一个LOL这样的游戏,使用面向对象的思想来设计,应该怎么做?

一、设计英雄类class

LOL有很多英雄,比如盲僧,团战可以输,提莫必须死,盖伦,琴女
所有这些英雄,都有一些共同的状态
比如,他们都有名字,hp,护甲,移动速度等等
这样我们就可以设计一种东西,叫做类,代表英雄这样一种事物
类: 英雄(Hero)
状态: 名字, 血量,护甲,移动速度

package com.code1401;

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

二、创建具体的英雄

类就像一个模板,在这个模板上我们创建很多对象。

new Hero()//其实就是创建英雄

package com.code1401;

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

    public static void main(String[] args) {
    
    
        //英雄一:盖亚
        Hero garden=new Hero();
        garden.name="盖伦";
        garden.hp=616.28f;
        garden.armor=27.536f;
        garden.moveSpeed=350;

        //英雄二:提莫
        Hero teemo=new Hero();
        teemo.name="提莫";
        teemo.hp=383f;
        teemo.armor=14f;
        teemo.moveSpeed=330;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43891901/article/details/122974327