java基础知识20_this关键字

this关键字:
this关键字代表了所属函数的调用者对象。
(说白了就是当存在同名的成员变量和局部变量时,想要取成员变量的值用时,用this关键字)

this关键字作用:

  • 1、如果存在同名成员变量与局部变量时,在方法内部默认是访问局部变量的数据,可以通过this关键字指定访问成员变量的数据。
  • 2、在一个构造函数中可以调用另外一个构造函数初始化对象。

this关键字调用其他的构造函数要注意的事项:

  • 1、this关键字调用其他的构造函数时,this关键字必须要位于构造函数中 的第一个语句。
  • 2、this关键字在构造函数中不能出现相互调用 的情况,因为是一个死循环。

this关键字要注意事项:

  • 1、存在同名的成员变量与局部变量时,在方法的内部访问的是局部变量(java 采取的是“就近原则”的机制访问的。)
  • 2、如果在一个方法中访问了一个变量,该变量只存在成员变量的情况下,那么java编译器会在该变量的前面自动添加this关键字。

练习1:
需求: 使用java类描述一个动物。

问题:存在同名的成员变量与局部变量时,在方法的内部访问的是局部变量(java 采取的是就近原则的机制访问的)。

class Anmial{
    String name = "狗";
    String color;
    public void eat(){
        String name = "老鼠";
        System.out.println(name+"在吃...");
    }
}
class Demo{
    public static void main(String[] args){
        Animal a = new Animal();
        a.eat();
    }
}
输出结果是:老鼠在吃...

当局部变量与成员变量存在同名时,内存中原理图分析如下:
这里写图片描述

以上例子若想输出“狗在吃…”,即name值取狗 不取老鼠,就要用到this关键字。

class Animal{
    String name ;  //成员变量
    String color;

    public Animal(String n , String c){
        name = n;
        color = c;
    }

    //this关键字代表了所属函数的调用者对象
    public void eat(){
        //System.out.println("this:"+ this);
        String name = "老鼠"; //局部变量
        System.out.println(this.name+"在吃..."); //需求: 就要目前的name是成员变量的name.   //当上一句【String name = "老鼠";】注释掉时,这句中的this关键字可省略。 
    }
}

class Demo6{
    public static void main(String[] args){
        Animal = new Animal("狗","白色");  //现在在内存中存在两份name数据。 
        //Animal cat = new Animal("猫","黑色");
        dog.eat();      
    }
}
//输出结果是:狗在吃...

举例:2:

class Student{
    int id;  //身份证
    String name;  //名字
    //目前情况:存在同名的成员变量与局部变量,在方法内部默认是使用局部变量的值。
    public Student(int id,String name){  //一个函数的形式参数也是属于局部变量。
        this(name); //调用了本类的 一个参数的构造方法
        //this(); //调用了本类的 无参的构造方法。
        this.id = id; // this.id = id 局部变量的id给成员变量的id赋值
        System.out.println("两个参数的构造方法被调用了...");
    }       

    public Student(){
        System.out.println("无参的构造方法被调用了...");
    }

    public Student(String name){
        this.name = name;
        System.out.println("一个参数的构造方法被调用了...");
    }   
}

class Demo7{
    public static void main(String[] args){
        Student s = new Student(110,"铁蛋");
        System.out.println("编号:"+ s.id +" 名字:" + s.name);
        /*      
        Student s2 = new Student("金胖子");
        System.out.println("名字:" + s2.name);
        */
    }
}

练习2:
需求: 使用java定义一个人类,人具备 id、name 、 age三个属性, 还具备一个比较年龄的方法。
要求: 必须 要 写上构造函数,构造函数也必须要使用上this关键字。

class Person{           
    int id; //编号    
    String name; //姓名     
    int age;  //年龄

    //构造函数
    public Person(int id,String name ,int age){
        this.id  = id;
        this.name = name;
        this.age = age;
    }

    //比较年龄的方法
    public void compareAge(Person p2){
        if(this.age>p2.age){
            System.out.println(this.name+"大!");
        }else if(this.age<p2.age){
            System.out.println(p2.name+"大!");
        }else{
            System.out.println("同龄");
        }
    }
}

class Demo8{    
    public static void main(String[] args){
        Person p1 = new Person(110,"狗娃",17);
        Person p2 = new Person(119,"铁蛋",9);
        p1.compareAge(p2);  
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值