面向对象基础
LOL有很多英雄,比如盲僧,团战可以输,提莫必须死,盖伦,琴女
所有这些英雄,都有一些共同的状态
比如,他们都有名字,hp,护甲,移动速度等等
这样我们就可以设计一种东西,叫做类,代表英雄这样一种事物
类: 英雄(Hero)
状态: 名字, 血量,护甲,移动速度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| class ltem//装备类 { String name; float price; } class Hero//英雄类 { String name; float hp; float armor; int moveSpeed; void output() { System.out.println("英雄名称:"+name+"\n英雄生命值:"+hp+"\n英雄护甲:"+armor+"\n移动速度:"+moveSpeed); } }
public class lol { public static void main(String[] args) { Hero timo=new Hero(); timo.name="提莫"; timo.hp=333.0f; timo.armor=14.0f; timo.moveSpeed=330; timo.output(); System.out.println("*****************************"); Hero gailun=new Hero(); gailun.name="盖伦"; gailun.hp=616.0f; gailun.armor=27.0f; gailun.moveSpeed=250; gailun.output(); System.out.println("*****************************"); ltem changjian=new ltem(); changjian.name="长剑"; changjian.price=350.0f; ltem xueping=new ltem(); xueping.name="血瓶"; xueping.price=50.0f; ltem caoxie=new ltem(); caoxie.name="草鞋"; caoxie.price=300.0f; System.out.println(changjian.name+"\n"+changjian.price+"\n"+xueping.name+"\n"+xueping.price+"\n"+caoxie.name+"\n"+caoxie.price); } }
|
