访问在另一个类中创建的对象
我在我的主类中创建一个线程。 该线程有一个在套接字上写入和读取的计时器。
我需要从声明它的地方(Main)之外的另一个类调用线程类中的方法,例如 writeSomething() 。
如何从另一个类引用该对象?
编辑
public static Thread connectionThread;
ModelJTable table = new ModelJTable();
connectionThread = new Thread(new ConnectionThread(table), "connectionThread");
connectionThread.start();
在线程类中有一个方法
public void openFile(String fileName){
String request = "open;" + fileName;
out.print(request);
}
我想
String open = "open;" + getname + ";" + getpath;
// This doesnt work
ConnectionThread.openFile(open);
,如果从另一个类(JTable 类)访问此调用会出现错误
无法访问 ConnectionThread 类型的封闭实例 范围
I create a thread in my main class.
The thread has a timer which writes and reads on a socket.
I need to call a method in the thread class e.g writeSomething() from another class outside of where it was declared(Main).
How is the object referenced from another class?
Edit
public static Thread connectionThread;
ModelJTable table = new ModelJTable();
connectionThread = new Thread(new ConnectionThread(table), "connectionThread");
connectionThread.start();
I have a method in the thread class
public void openFile(String fileName){
String request = "open;" + fileName;
out.print(request);
}
I want to access if from another class(the JTable class)
String open = "open;" + getname + ";" + getpath;
// This doesnt work
ConnectionThread.openFile(open);
This call gives an error
No enclosing instance of the type ConnectionThread is accessible in
scope
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要么将其传递到第二类的构造函数中,要么在第一类中将其设为静态,或者以
1: static one 的方式序列化它
Either pass it in constructor of second class OR make it static in first class, OR serialize it
way 1: static one
将 Thread 的引用传递给需要调用该方法的类。
Pass a reference to the Thread to the class that needs to call the method.