java中从main方法调用另一个方法

发布于 2024-10-15 02:17:20 字数 315 浏览 4 评论 0原文

我有

class foo{

   public static void main(String[] args){
      do();
   }

   public void do(){}


}

,但是当我通过在命令行上运行命令 java foomain 调用 do() 时,java 抱怨你不能' t 从静态函数调用方法。

所以我的问题是:如何从主方法调用方法,如果不可能,在使用 java 命令从命令行运行程序后调用方法有哪些替代策略。

I have

class foo{

   public static void main(String[] args){
      do();
   }

   public void do(){}


}

but then when I call do() from main by running the command java foo on the command line, java complains that you can't call a method from a static function.

So my question is: How do you call methods from the main method and if it is not possible what are some alternative strategies to call methods after the program is run from the command line using the java command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

锦上情书 2024-10-22 02:17:20

您只能针对类的实例调用像 do() 这样的实例方法(顺便说一句,这是一个非法的方法名称):

public static void main(String[] args){
  new Foo().doSomething();
}

public void doSomething(){}

或者,也可以将 doSomething() 设为静态,如果这适合您的设计。

You can only call instance method like do() (which is an illegal method name, incidentally) against an instance of the class:

public static void main(String[] args){
  new Foo().doSomething();
}

public void doSomething(){}

Alternatively, make doSomething() static as well, if that works for your design.

审判长 2024-10-22 02:17:20

检查 main 方法之前的 static ,这将该方法声明为类方法,这意味着它不需要调用实例。因此,当您要调用非静态方法时,Java 会抱怨,因为您正在尝试调用所谓的“实例方法”,这当然首先需要一个实例;)

如果您想更好地了解类和实例,请创建一个带有实例和类方法的新类,在主循环中创建一个对象并调用方法!

 class Foo{

    public static void main(String[] args){
       Bar myInstance = new Bar();
       myInstance.do(); // works!
       Bar.do(); // doesn't work!

       Bar.doSomethingStatic(); // works!
    }
 }

class Bar{

   public do() {
   // do something
   }

   public static doSomethingStatic(){
   }
}

另请记住,Java 中的类应以大写字母开头。

Check out for the static before the main method, this declares the method as a class method, which means it needs no instance to be called. So as you are going to call a non static method, Java complains because you are trying to call a so called "instance method", which, of course needs an instance first ;)

If you want a better understanding about classes and instances, create a new class with instance and class methods, create a object in your main loop and call the methods!

 class Foo{

    public static void main(String[] args){
       Bar myInstance = new Bar();
       myInstance.do(); // works!
       Bar.do(); // doesn't work!

       Bar.doSomethingStatic(); // works!
    }
 }

class Bar{

   public do() {
   // do something
   }

   public static doSomethingStatic(){
   }
}

Also remember, classes in Java should start with an uppercase letter.

梦毁影碎の 2024-10-22 02:17:20

这是 Java 的基本理解,但对于新程序员来说可能有点棘手。对静态方法和实例方法之间的区别进行一些研究。基本区别是实例方法 do() 只能由类 foo 的实例访问。

您必须实例化该类(创建其实例),创建一个用于调用实例方法的对象。

我已将您的示例包含在一些评论和示例中。

public class SomeName {

//this is a static method and cannot call an instance method without a object
public static void main(String[] args){

    // can't do this from this static method, no object reference
    // someMethod();

    //create instance of object
    SomeName thisObj = new SomeName();
    //call instance method using object
    thisObj.someMethod();
}

//instance method
public void someMethod(){
    System.out.print("some message...");
}

}// end class SomeName

This is a fundamental understanding in Java, but can be a little tricky to new programmers. Do a little research on the difference between a static and instance method. The basic difference is the instance method do() is only accessible to a instance of the class foo.

You must instantiate (create an instance of) the class, creating an object, that you use to call the instance method.

I have included your example with a couple comments and example.

public class SomeName {

//this is a static method and cannot call an instance method without a object
public static void main(String[] args){

    // can't do this from this static method, no object reference
    // someMethod();

    //create instance of object
    SomeName thisObj = new SomeName();
    //call instance method using object
    thisObj.someMethod();
}

//instance method
public void someMethod(){
    System.out.print("some message...");
}

}// end class SomeName
请远离我 2024-10-22 02:17:20

您可以通过多种方式做到这一点。这里有两个。干杯!

package learningjava;

public class helloworld {
    public static void main(String[] args) {
        new helloworld().go();
        // OR
        helloworld.get();
    }

    public void go(){
        System.out.println("Hello World");
    }
    public static void get(){
        System.out.println("Hello World, Again");
    }
}

You can do it multiple ways. Here are two. Cheers!

package learningjava;

public class helloworld {
    public static void main(String[] args) {
        new helloworld().go();
        // OR
        helloworld.get();
    }

    public void go(){
        System.out.println("Hello World");
    }
    public static void get(){
        System.out.println("Hello World, Again");
    }
}
空心空情空意 2024-10-22 02:17:20

如果你想在你的 main 方法中使用 do() ,有 2 个选择,因为一个是静态的,但另一个 (do()) 不

  1. 创建新实例并调用 do() 就像 new Foo( ).do();
  2. make static do() 方法

看看这个 Sun 教程

If you want to use do() in your main method there are 2 choices because one is static but other (do()) not

  1. Create new instance and invoke do() like new Foo().do();
  2. make static do() method

Have a look at this sun tutorial

江挽川 2024-10-22 02:17:20

首先java不允许你有do()方法。相反,您可以将其设为 doOperation()

其次,您不能从静态函数直接调用非静态方法。 Main 是一个静态函数。您需要首先实例化该类,然后使用该实例调用方法。

第三,您可以直接从非静态方法调用静态方法。

First java will not allow you to have do() method. Instead you can make it doOperation().

Second You cann't invoke directly non static methods from static function. Main is a static function. You need to instantiate the class first and then invoke method using that instance.

Third you can invoke static method directly from non static methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文