It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center
                            
                        
                    
                
                7年前关闭。
            
        

即使不创建Runner对象,我也可以从线程的run方法访问Runner类中的process方法吗?

class Runner {
  public void process() {
    // some multithreaded code
  }
}

main() {
Thread t1 = new Thread(new Runnable() {
                public void run() {
                    process();
                }
           });
t1.start();
}

最佳答案

您可以将其设置为static

class Runner {
      public static void process() {
        // some multithreaded code
      }
    }


然后:

public static void main() {
    Thread t1 = new Thread(new Runnable() {
                public void run() {
                    Runner.process();
                }
           });
    t1.start();
}


但是,如果没有process()限定条件或没有实例化,将无法引用Runner.方法。这是因为JavaObject Oriented语言。

09-11 19:38