当前位置: 移动技术网 > IT编程>开发语言>Java > Java自学-类和对象 this

Java自学-类和对象 this

2019年08月30日  | 移动技术网IT编程  | 我要评论

java 中的 this

this 这个关键字,相当于普通话里的“

小明说 “我吃了” 这个时候,“我” 代表小明
小红说 “我吃了” 这个时候,“我” 代表小红
"我"代表当前人物

步骤 1 : this代表当前对象

this代表当前对象

public class hero {
     
    string name; //姓名
     
    float hp; //血量
     
    float armor; //护甲
     
    int movespeed; //移动速度
 
    //打印内存中的虚拟地址
    public void showaddressinmemory(){
        system.out.println("打印this看到的虚拟地址:"+this);
    }
     
    public static void main(string[] args) {
        hero garen =  new hero();
        garen.name = "盖伦";
        //直接打印对象,会显示该对象在内存中的虚拟地址
        //格式:hero@c17164 c17164即虚拟地址,每次执行,得到的地址不一定一样
 
        system.out.println("打印对象看到的虚拟地址:"+garen);
        //调用showaddressinmemory,打印该对象的this,显示相同的虚拟地址
        garen.showaddressinmemory();
         
        hero teemo =  new hero();
        teemo.name = "提莫";
        system.out.println("打印对象看到的虚拟地址:"+teemo);
        teemo.showaddressinmemory();
    }  
     
}

步骤 2 : 通过this访问属性

通过this关键字访问对象的属性

public class hero {
     
    string name; //姓名
     
    float hp; //血量
     
    float armor; //护甲
     
    int movespeed; //移动速度
 
    //参数名和属性名一样
    //在方法体中,只能访问到参数name
    public void setname1(string name){
        name = name;
    }
     
    //为了避免setname1中的问题,参数名不得不使用其他变量名
    public void setname2(string heroname){
        name = heroname;
    }
     
    //通过this访问属性
    public void setname3(string name){
        //name代表的是参数name
        //this.name代表的是属性name
        this.name = name;
    }
     
    public static void main(string[] args) {
        hero  h =new hero();
         
        h.setname1("teemo");
        system.out.println(h.name);
         
        h.setname2("garen");
        system.out.println(h.name);    
         
        h.setname3("死歌");
        system.out.println(h.name);    
    }
     
}

步骤 3 : 通过this调用其他的构造方法

如果要在一个构造方法中,调用另一个构造方法,可以使用this()

public class hero {
        
    string name; //姓名
        
    float hp; //血量
        
    float armor; //护甲
        
    int movespeed; //移动速度
        
    //带一个参数的构造方法
    public hero(string name){
        system.out.println("一个参数的构造方法");
        this.name = name;
    }
      
    //带两个参数的构造方法
    public hero(string name,float hp){
        this(name);
        system.out.println("两个参数的构造方法");
        this.hp = hp;
    }
 
    public static void main(string[] args) {
        hero teemo =  new hero("提莫",383);
         
        system.out.println(teemo.name);
         
    }
      
}

练习

(设计一个构造方法,参数分别是

string name
float hp
float armor
int movespeed

不仅如此,在这个构造方法中,调用这个构造方法

public hero(string name,float hp))

答案

public class hero {
 
    string name; // 姓名
 
    float hp; // 血量
 
    float armor; // 护甲
 
    int movespeed; // 移动速度
 
    // 带一个参数的构造方法
    public hero(string name) {
        system.out.println("一个参数的构造方法");
        this.name = name;
    }
 
    // 带两个参数的构造方法
    public hero(string name, float hp) {
        this(name);
        system.out.println("两个参数的构造方法");
        this.hp = hp;
    }
 
    // 带有四个参数的构造方法
    public hero(string name, float hp, float armor, int movespeed) {
        this(name,hp);
        this.armor = armor;
        this.movespeed = movespeed;
    }
 
    public static void main(string[] args) {
        hero teemo = new hero("提莫", 383);
        system.out.println(teemo.name);
        hero db =  new hero("死哥",400,27,360);
        system.out.println(db.movespeed);
    }
 
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网