我知道这this是指当前的对象.但我不知道什么时候才能真正使用它.例如,如果我使用x而不是this.x在某些方法中,会有什么区别吗?可能x会引用一个考虑方法的局部变量?我的意思是仅在此方法中看到的变量.
怎么样this.method()?我可以用吗?我应该用它吗?如果我只是使用method(),默认情况下它不会应用于当前对象吗?
我的老师说,当我尝试访问方法中的实例变量时,我应该总是使用this关键字,否则我会执行双重搜索.本地范围搜索,然后是实例范围搜索.
例:
public class Test(){
int cont=0;
public void Method(){
System.out.println(cont);//Should I use This.cont instead?
}
}
Run Code Online (Sandbox Code Playgroud)
我希望他错了,但我找不到任何论据.
可能重复:
Java - 何时使用'this'关键字
一些开发人员喜欢在引用对象的方法和属性时总是使用"this",即使不需要它也是如此.这是一个非常简单的例子:
public class Foo {
private String id;
public Foo() {
this.id = "123456789";
}
}
Run Code Online (Sandbox Code Playgroud)
总是使用"这个"或仅在必要时才更清楚吗?在任何情况下你应该经常使用它吗?
this在Java中使用关键字optional吗?或者我必须使用它吗?当它不是可选的?在下面的代码中,它不会影响我的应用程序,无论Employee我做了多少个实例.print即使细节不匹配,该方法也会打印员工详细信息而不会出现任何问题.
public class Employee {
String name;
int Salary;
int pension;
String workPlace;
String teleNo;
int age;
void printDetails(){
System.out.println("Name is : "+this.name );
System.out.println("age is : "+this.age );
System.out.println("WorkPlace is : "+this.workPlace );
System.out.println("Salary is : "+Salary );
System.out.println("Pension is : "+this.pension );
System.out.println("Telephone No. is : "+this.teleNo );
System.out.println("age is : "+Integer.toString(age) );
}
}
public class Main extends Employee {
/**
* @param args
*/
public static void main(String[] args) {
Employee …Run Code Online (Sandbox Code Playgroud) 我不明白这两个代码之间的真正区别,尽管它们都有效.
如果我使用这个类:
public class City {
private String name;
Run Code Online (Sandbox Code Playgroud)
我不明白这种方法的区别:
public String getName(){
return this.name;
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
public String getName(){
return name;
}
Run Code Online (Sandbox Code Playgroud)
这两种方法有效,但哪种方法最好用,为什么它们都有效?
谢谢你的回答.
我已经设置了这样的构造函数:
public class VendingMachine {
private double currentBalance;
private double itemPrice;
private double totalCollected;
public VendingMachine(double itemCost) {
currentBalance = 0;
totalCollected = 0;
itemPrice = itemCost;
}
...
}
Run Code Online (Sandbox Code Playgroud)
我的问题是通过接受double的参数来设置我的构造函数有什么不同itemCost.
有什么不同而不是做到:
this.itemPrice = itemCost;
Run Code Online (Sandbox Code Playgroud) var = putVar;以下代码运行 ; & this.var = putVar;
我理解:"this"用于识别 - "将此值仅用于'我的'对象".当两者都有效时,为什么人们通常在制定者中使用"这个"?
码:
public class PlayingWithObjects
{
public static void main(String[] args)
{
SomeClass classObj = new SomeClass(10);
System.out.println("classObj.getVar: " + classObj.getVar() );
classObj.setVar(20);
System.out.println("classObj.getVar: " + classObj.getVar() );
classObj = new SomeClass(30);
System.out.println("classObj.getVar: " + classObj.getVar() );
}
}
class SomeClass
{
private int var;
public SomeClass(int putVar)
{
var = putVar;
}
public int getVar()
{
return var;
}
public void setVar(int putVar)
{
// var = putVar; …Run Code Online (Sandbox Code Playgroud) 假设我创建了这个类:
public class Panel extends JPanel{
private JTextBox textBox;
public Panel(){
this.textBox = new JTextBox(10);
add(this.textBox);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的主要:
public class Main{
public static void main(String[] args){
Panel panel1 = new Panel();
Panel panel2 = new Panel();
}
}
Run Code Online (Sandbox Code Playgroud)
在课堂上Panel,是否有必要this在每一行打电话,还是可以把它丢弃?或者它会弄乱Panels?