美文网首页
重捡Java(七)类与对象 this

重捡Java(七)类与对象 this

作者: 我很惊讶 | 来源:发表于2020-05-10 21:40 被阅读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);
    }
}

相关文章

网友评论

      本文标题:重捡Java(七)类与对象 this

      本文链接:https://www.haomeiwen.com/subject/bivsnhtx.html