变量有两种类型 基本类型 和类类型
public class Hero { String name; //姓名 float hp; //血量 float armor; //护甲 int moveSpeed; //移动速度 public Hero(){ } //回血 public void huixue( int xp){ hp = hp + xp; //回血完毕后,血瓶=0 xp= 0 ; } public Hero(String name, float hp){ this .name = name; this .hp = hp; } public static void main(String[] args) { Hero teemo = new Hero( "提莫" , 383 ); //血瓶,其值是100 int xueping = 100 ; //提莫通过这个血瓶回血 teemo.huixue(xueping); System.out.println(xueping); } } |
public class Hero { String name; // 姓名 float hp; // 血量 float armor; // 护甲 int moveSpeed; // 移动速度 public Hero(String name, float hp) { this .name = name; this .hp = hp; } // 攻击一个英雄,并让他掉damage点血 public void attack(Hero hero, int damage) { hero.hp = hero.hp - damage; } public static void main(String[] args) { Hero teemo = new Hero( "提莫" , 383 ); Hero garen = new Hero( "盖伦" , 616 ); garen.attack(teemo, 100 ); System.out.println(teemo.hp); } } |
原文:https://www.cnblogs.com/Lanht/p/12441267.html