重捡Java(七)类与对象 this

2020-05-10  本文已影响0人  我很惊讶

简单一说,this指当前对象,也可以指当前对象的构造方法

public class Hero {
    String name; //姓名
    
    float hp; //血量
       
    float armor; //护甲
       
    int moveSpeed; //移动速度
     
    public Hero(String name,float hp,float armor,int moveSpeed){
        this(name,hp);
        System.out.println("四个参数的构造方法");
        this.armor = armor;
        this.moveSpeed = moveSpeed;
    }
    public Hero(String name,float hp){
        System.out.println("两个参数的构造方法");
        this.name = name;
        this.hp = hp;
    }
    public static void main(String[] args){
        Hero teemo = new Hero("提莫",383,25,300);
         
        System.out.println(teemo.name);
    }
}
上一篇 下一篇

猜你喜欢

热点阅读