早些时候,当内部匿名类没有看到“外部”类的字段时,我遇到了问题。我需要创建一个final变量,使其对内部类可见。现在我的情况相反。在“外部”类“ClientListener”中,我使用内部类“Thread”和“Thread”类,我使用“run”方法,并且确实从“外部”类中看到“earPort”!为什么呢?
import java.io.IOException;
import java.net.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class ClientsListener {
private int earPort;
// Constructor.
public ClientsListener(int earPort) {
this.earPort = earPort;
}
public void starListening() {
Thread inputStreamsGenerator = new Thread() {
public void run() {
System.out.println(earPort);
try {
System.out.println(earPort);
ServerSocket listeningSocket = new ServerSocket(earPort);
Socket serverSideSocket = listeningSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(serverSideSocket.getInputStream()));
} catch (IOException e) {
System.out.println("");
}
}
};
inputStreamsGenerator.start();
}
}
答案 0 :(得分:12)
匿名内部类可以访问静态和实例变量。如果您还想访问局部变量,请将它们声明为final。这是它的工作原理:))
答案 1 :(得分:1)
您的匿名内部类可以访问包含对象的属性。所有未声明为static
的内部类都具有隐式访问器。
如果要防止这种情况发生,可以声明一个静态内部类并实例化:
public class ClientsListener {
private int earPort;
// Constructor.
public ClientsListener(int earPort) {
this.earPort = earPort;
}
public void starListening() {
Thread inputStreamsGenerator = new InputStreamsGenerator();
inputStreamsGenerator.start();
}
private static class InputStreamsGenerator extends Thread() {
public void run() {
// no access to earport in next line (can make it a constructor argument)
System.out.println(earPort);
try {
System.out.println(earPort);
ServerSocket listeningSocket = new ServerSocket(earPort);
Socket serverSideSocket = listeningSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(serverSideSocket.getInputStream()));
} catch (IOException e) {
System.out.println("");
}
}
};
}