我试图理解this
java中的关键字。我想改用this
关键字来重写这段代码。如果我做得对,请告诉我。这是原始代码:
public class Book {
private String title;
private String author;
private String publisher;
public Book(String bookTitle, String authorName, String publisherName){
title = bookTitle;
author = authorName;
publisher = publisherName;
}
}
这是重写的代码:
public class Book {
private String title;
private String author;
private String publisher;
public Book(String title, String author, String publisher){
this.title = title;
this.author = author;
this.publisher = publisher;
}
}
我做对了吗?
谢谢,
凯文
编辑:感谢您的回复......还有一个问题:在修改后的代码的构造函数中,等号的哪一侧指的是类变量?例如, inthis.title = title;
是this.title
指title
来自构造函数还是来自类变量的变量?
根据下面的回复,我认为答案是this.title
指类变量title
。