美文网首页
this关键字

this关键字

作者: 落在牛背上的鸟 | 来源:发表于2018-03-08 00:36 被阅读44次
  1. this 调用属性;
  2. this调用方法
  3. 利用this表示当前对象

1. 调用属性

范例:观察代码

class Book {
    private String title;
    private double price;
    public Book(String t , double p) {
        title = t;
        price = p;
    }
    //getter , setter 省略
    public String getInfo() {
        return "书名:" + title + "; 价格:" + price ;
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Book book = new Book("Java" , 89.0);
        System.out.println(book.getInfo());
    }
}

注意构造方法

public Book(String t , double p) {
  title = t;
  price = p;
}

这个构造方法主要功能是为title和price两个属性初始化,为了更直观,方法中的形参名称可以做修改,直接将参数名称改为属性名称一致。如:

public Book(String title , double price ) {
  title = title ;
  price = price ;
}

修改完成后,运行代码发行,构造方法传递的参数内容没有传递到属性中。
因为再Java程序里面都是以“{}”为界限,如果现在属性名称与参数名称出现了重名的情况下,如果没有加入任何的限制,值得都是最近的"{}"内的变量名称。所以再这种情况下为了可以明确的找到要访问的变量属于类中的属性的时候,需要再变量前面加上this,这样就可以准确的进行性的标记。

public Book(String title , double price ) {
  this.title = title ;
  this.price = price ;
}

在以后的开发中,只要是访问类中的属性前面必须加上''this''

2. 调用方法

  1. 调用普通方法:
class Book {
    private String title;
    private double price;
    public Book(String t , double p) {
        title = t;
        price = p;
    }
    //getter , setter 省略
    public void print() {
        System.out.println("**************");
    }
    public String getInfo() {
        this.print();
        return "书名:" + title + "; 价格:" + price ;
    }
}

其中,this.print() 调用普通方法,关于调用普通方法时候需要this,并没有明确的要求,即使不加this也表示本类调用的放a,但是为了代码的严谨性,一定要加上this。

  1. 调用构造方法
    范例:观察问题
    现在定义Book类里面有三个构造方法,没个方法都要输出一行提示纤细"新的Book类对象产生"。
class Book {
    private String title ;
    private double price ;
    public Book() {
        System.out.println("新的Book类对象产生");
    }
    public Book(String t) {
        System.out.println("新的Book类对象产生");
        this.title=t ;
    }
    public Book(String t , double p) {
        System.out.println("新的Book类对象产生");
        this.title = t ;
        this.price = p ;
    }
    //setter , getter 方法省略
    public String Info() {
       return "书名:" + this.title + " ,价格: " + this.price ;
    }
}

public class thisTest {
    public static void main(String[] args) {
        Book book = new Book();
        System.out.println(book.Info());
    }
}

观察可以发现,有重复代码,消除重复代码后:

class Book {
    private String title ;
    private double price ;
    public Book() {
        System.out.println("新的Book类对象产生");
    }
    public Book(String t) {
        this() ;    //调用本类的无参构造
        this.title=t ;
    }
    public Book(String t , double p) {
        this(t) ;   //调用本类的单参构造
        this.title = t ;
        this.price = p ;
    }
    //setter , getter 方法省略
    public String Info() {
       return "书名:" + this.title + " ,价格: " + this.price ;
    }
}

public class thisTest {
    public static void main(String[] args) {
        Book book = new Book();
        System.out.println(book.Info());
    }
}

以上实现了构造方法的互相调用,但是还要注意两点:

  • 使用 "this()"调用构造方法形式的代码只能够放在构造方法的首行;
  • 进行构造方法互相调用的时候,一定要保留调用的出口。

3. 表示当前对象

所谓的当前对象指的就是当前正在调用类中方法的对象。
示例

class Book {
    public void print() {
        System.out.println("this =  " + this);
    }
}
public class thisTest {
    public static void main(String[] args) {
        Book book_1 = new Book();
        System.out.println("book_1 = " + book_1);
        book_1.print();
        System.out.println("------------------");
        Book book_2 = new Book();
        System.out.println("book_2 = " + book_2);
        book_2.print();
    }
}

示例结果

book_1 = thisDemo.Book@4554617c
this =  thisDemo.Book@4554617c
------------------
book_2 = thisDemo.Book@74a14482
this =  thisDemo.Book@74a14482

可以发现book_1 和 book_2 的内存地址不一样。但是哪个对象调用了print的方法,this就自动与此对象指向同一内存地址,实际上this就是当前调用方法的对象

相关文章

网友评论

      本文标题:this关键字

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