从另一个类访问Class的实例

时间:2012-09-06 21:38:25

标签: java

我正在使用API​​编写项目。 总的来说,我的程序看起来就像这样。

public class Programm extends bridge {
Client Eclient = new Client() ;
public static void main(String[] args) {
Programm MyProgramm = new Programm();
MyProgramm.MyMethod();
public void MyMethod(){
 Runnable countdown = new flipper();
 Thread worker = new Thread(countdown);
 worker.start();
}
}
abstract bridge implements API{}
class flipper implements runnable {
public void run(){MyProgramm.Eclient.ApiMethod()}
}

Basiclly我需要访问EClient类实例,因为它将从类鳍状肢中的MyProgramm实例接收

2 个答案:

答案 0 :(得分:0)

class flipper implements Runnable {
private ESocket lEClient;
   public flipper (Object pEClient) {
     lEClient = pEClient;
   }

然后你调用像这样的构造函数:

public void MyMethod()
 {

   Runnable countdown = new flipper(Eclient);

  Thread worker = new Thread(countdown);
   worker.start();
 }

public void run() {lEClient .Apimethod()}

答案 1 :(得分:0)

选项2:使Flipper成为内部类

public class Program extends bridge {
   Client eClient = new Client() ;
   public void main(String[] args) {...}
   public void myMethod(){...}
   class Flipper implements Runnable {
      public void run(){Program.eClient.apiMethod()}
   }
}