gpt4 book ai didi

Java:类.this

转载 作者:IT老高 更新时间:2023-10-28 11:26:29 24 4
gpt4 key购买 nike

我有一个类似这样的 Java 程序。

public class LocalScreen {

public void onMake() {
aFuncCall(LocalScreen.this, oneString, twoString);
}
}

LocalScreen.thisaFuncCall中是什么意思?

最佳答案

LocalScreen.this 引用封闭类的this

这个例子应该解释一下:

public class LocalScreen {

public void method() {

new Runnable() {
public void run() {
// Prints "An anonymous Runnable"
System.out.println(this.toString());

// Prints "A LocalScreen object"
System.out.println(LocalScreen.this.toString());

// Won't compile! 'this' is a Runnable!
onMake(this);

// Compiles! Refers to enclosing object
onMake(LocalScreen.this);
}

public String toString() {
return "An anonymous Runnable!";
}
}.run();
}

public String toString() { return "A LocalScreen object"; }

public void onMake(LocalScreen ls) { /* ... */ }

public static void main(String[] args) {
new LocalScreen().method();
}
}

输出:

An anonymous Runnable!
A LocalScreen object

本帖已改写为文章 here .

关于Java:类.this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5530256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com