我对java类和驱动程序真的很困惑。驱动程序代码的语法是什么,里面应该有什么,不应该有什么?对于驱动程序,我们可能需要在其中包含一个主程序,但是 java 类呢,我们不需要任何...吗? 我需要在 java 类和驱动程序中都使用这个还是只在驱动程序中使用:
public static void main(String[] args) {
最佳答案
引用自这里:
What is a driver class? (Java)
A "Driver class" is often just the class that contains a main. In a real project, you may often have numerous "Driver classes" for testing and whatnot, or you can build a main into any of your objects and select the runnable class through your IDE, or by simply specifying "java classname."
例子:
这不是驱动程序类,因为它不包含任何主要方法。在这种情况下,它有方法“hello”:
public class HelloWorld {
public void hello() {
System.out.println("Hello, world!");
}
}
与此相对 - 这是一个驱动程序类,因为它包含一个主要方法,并且是运行 HelloWorld 的类:
public class HelloWorldDriver {
public static void main(String[] args) {
HelloWorld sayhello = new HelloWorld();
sayhello.hello();
}
}
因此得名“驱动程序类”- 作为类 HelloWorldDriver“驱动”或者更确切地说,控制类 HelloWorld 的实例化和使用。
关于Java驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15318431/