JAVA:this的用法与应用

1.在构造器(构造函数)中初始化成员变量

  • 调用本类的属性,即本类的成员变量,进行初始化操作。 且this代表当前所创建的实例对象。
  • 以我的上一篇博客“ JAVA——Object类,equals方法”中的代码为示例:
class Student 
{
    private String name;
    private int age;

    public Student(String name, int age)
    {
        this.name = name;/*this代表当前所创建的实例对象。*/
        this.age = age;/*this.name可以直观的区分形参name与本类的成员变量name,同时this.age也是一样。*/
    }
    /*在自定义类中使用equals方法,就必须在自定义类中覆盖Object类的equals方法。*/
    public boolean equals(Object obj) 
    {
        Student st = null;

        if(obj instanceof Student)
            st = (Student)obj;
        else
            return false;

        if(st.name == this.name && st.age == this.age)//this代表当前所调用该方法的实例对象。
            return true;
        else
            return false;
    }

}
public class TestStudent
{
    public static void main(String[] args) 
    {
        Student stu1 = new Student("hoqian",7);
        Student stu2 = new Student("hoqian",7);//("kangrn",6);

        /*如果把equals方法注销了,程序执行结果为:stu1 与 stu2 不相等! */

        if(stu1.equals(stu2))
            System.out.println("stu1 与 stu2 相等!");
        else
            System.out.println("stu1 与 stu2 不相等!");
    }
}

2.在方法内部表示对“调用方法的那个对象”的引用

  • 在方法内部,this表示对“调用方法的那个对象的引用”。
class Student 
{
    private String name;
    private int age;

    public Student(String name, int age)
    {
        this.name = name;
        this.age = age;
    }

    public void showStudent()
    {
        System.out.println("name = " + name + " " + "age = " + age);/*语句1*/
    }
}
  • 对于“语句1”,可以写成
    System.out.println("name = " + this.name + " " + "age = " + this.age);

  • 但是,在实际编写代码的时候不需要这样添加 this 。因为 在方法内部调用同一个类的一个成员变量,就不必使用this,直接调用即可,编译器会自动刚我们添加。

class Student
{
   pubilc void inforStudent()
   {
       /* ... */
   }
   public void getInforStudent()
   {
       inforStudent();/*
       /* ... */
   }
}
  • “语句2”等价于:this.inforStudent();
  • 但是,在实际编写代码的时候不需要这样添加 this 。因为 在方法内部调用同一个类的另一个方法,就不必使用this,直接调用即可,编译器会自动刚我们添加。

3.作为返回值返回对当前对象的引用

  • 只有当需要明确指出对当前对象的引用时,才需要使用this关键字。
  • 当需要返回对当前对象的引用时,常常在return后添加this关键字。
 public class Leaf
 {
    int i = 0;
    Leaf increment()
    {
        i++;
        return this;
    }
    void print()
    {
        System.out.println("i = " + i);
    }
    public static void main(String[] args)
    {
        Leaf x = new Leaf();
        x.increment().increment().increment().print();
    }
 }
  • 输出结果为: i = 3
  • 由于increment()通过this关键字返回了当前对象的引用,所以很容易在一条语句里对同一个对象执行多次操作。

4.将this作为当前对象的引用传递给其他方法

  • 将当前对象传递给其他方法时,可以使用this关键字。
class Person
{
    public void eat(Apple apple)
    {
        Apple peeled = apple.getPeeled();
    }
}
class Peeler
{
    static Apple peel(Apple apple)
    {
        //...remove peel
        return apple;
    }
}
class Apple
{
    Apple getPeeled()
    {
        return Peeler.peel(this);
    }
}
public class PassingThis
{
    public static void main(String[] args)
    {
        new Person().eat(new Apple());
    }
}

5.在构造器中调用构造器

  • 注意:1-4中的this都是指“这个对象”或“当前对象”,它本身表示对当前对象的引用。
  • 在构造器中,如果为this添加了参数列表,这时又有不同的含义。
class Student 
{
    private String name;
    private int age;

    public Student(String name)
    {
        this.name = name;
    }
    public Student(int age)
    {
        this.age = age;
    }
    public Student(String name, int age)
    {
        /*方法一:正确*/
        this.name = name;//this代表当前所创建的实例对象。
        this.age = age;


        /*方法二:错误
        this(name);
        this(age);//对this的调用,只能有一个this构造器语句,且必须是构造器(构造函数)中的第一个语句。
        */

        /*方法三:错误
        this.age = age;
        this(name);//对this的调用,必须是构造器(构造函数)中的第一个语句。
        */

        /*方法四:正确
        this(name);
        this.age = age;
        */
    }

    public boolean equals(Object obj)//在自定义类中使用equals方法,就必须在自定义类中覆盖Object类的equals方法。
    {
        Student st = null;

        if(obj instanceof Student)
            st = (Student)obj;
        else
            return false;

        if(st.name == this.name && st.age == this.age)//this代表当前所调用该方法的实例对象。
            return true;
        else
            return false;
    }

}
public class TestStudent
{
    public static void main(String[] args) 
    {
        Student stu1 = new Student("hoqian",7);
        Student stu2 = new Student("hoqian",7);//("kangrn",6);

        /*如果把equals方法注销了,程序执行结果为:stu1 与 stu2 不相等! */

        if(stu1.equals(stu2))
            System.out.println("stu1 与 stu2 相等!");
        else
            System.out.println("stu1 与 stu2 不相等!");
    }
}

6.this使用注意

  • 在同一个构造器中,尽管可以用this调用构造器,但是不能调用两个this构造器,只能调用一个。
  • 必须将this构造器调用语句放置在构造器(构造函数)体的第一行,否则编译器会出错。
  • 除了构造器之外,编译器禁止在其他任何地方调用构造器。

参考文献:java编程思想,java就业培训教程

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值