当我第一次开始编程时,我在main中编写了所有内容.但是据我所知,我试图在我的main()方法中尽可能少地做.
但是你在哪里决定让其他班级/方法有责任接管该计划main()?你怎么做呢?
我已经看到很多方法,像这样:
class Main
{
public static void main(String[] args)
{
new Main();
}
}
Run Code Online (Sandbox Code Playgroud)
还有一些像:
class Main {
public static void main(String[] args) {
GetOpt.parse(args);
// Decide what to do based on the arguments passed
Database.initialize();
MyAwesomeLogicManager.initialize();
// And main waits for all others to end or shutdown signal to kill all threads.
}
}
Run Code Online (Sandbox Code Playgroud)
应该和不应该做什么main()?或者没有银子弹?
谢谢你的时间!
使用main方法测试java/.net类是一个好习惯吗?
我在一些教科书中看到它被推荐,但对我而言,使用单元测试框架似乎更有意义......
main方法为您提供了一个入口点,您可以测试类功能的一个方面.你可以猜测很多,但它似乎没有使用Junit或Nunit那么有意义.
所以我想测试Void类型然后我写了这个简单的程序:
package ehsan;
public class NumTest {
public static Void main(String[] args) {
System.out.println("Hello, World!");
return null; /* The compiler forced me to do so. I just can't realize what is the point in returning in Void type!? */
}
}
Run Code Online (Sandbox Code Playgroud)
所以现在当我想编译时,编译器抱怨:
main method must return a value of type void
Run Code Online (Sandbox Code Playgroud)
为什么编译器看不到我什么也没回来并且正在使用Void?
我有一个 Gradle 项目(例如,MyProject),它有一个 main 方法,我只想用它来运行一个小型 Java 应用程序。比如说,脚本位于一个类和包中,如下所示:
MyScript.Java
package com.car.ant;
import com.3rdparty.library.LibraryException;
public class MyScript {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Run Code Online (Sandbox Code Playgroud)
我用 构建项目$ ./gradlew clean build,没问题。我试图通过命令行运行它,但是当我这样做时,我在尝试运行时遇到异常:
$ pwd
~/workspace/MyProject/build/classes/java/main
$ java com.car.ant.MyScript
Exception in thread "main" java.lang.NoClassDefFoundError: com/3rdparty/library/exception/LibraryException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
... 7 more
Run Code Online (Sandbox Code Playgroud)
该脚本不是应用程序的主要部分,但我只是希望它偶尔运行一次,并且我想弄清楚如何通过命令行运行它。有没有办法在不导入更多库的情况下做到这一点?我见过用一罐子完成的工作,但我想我可以不用一罐子也能做到这一点。
更新:
阅读本文后尝试添加类路径,但现在我收到了不同的错误:
$ java -cp com/3rdparty/library/exception/LibraryException com.car.ant.MyScript
Error: Could not find or load main class com.car.ant.MyScript
Run Code Online (Sandbox Code Playgroud) 我想定义一个基类,它定义一个实例化类的main方法,并运行一个方法.但是有几个问题.这是基类:
public abstract class Strategy
{
abstract void execute(SoccerRobot robot);
public static void main(String args)
{
Strategy s = new /*Not sure what to put here*/();
s.execute(new SoccerRobot())
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个示例派生类:
public class UselessStrategy
{
void execute(SoccerRobot robot)
{
System.out.println("I'm useless")
}
}
Run Code Online (Sandbox Code Playgroud)
它定义了一个简单的execute方法,在用作主应用程序时应该在main方法中调用.但是,为了做到这一点,我需要在基类的main方法中实例化派生类.这似乎不可能.
我宁愿不必为每个派生类重复main方法,因为它感觉有点不合时宜.
有没有正确的方法呢?
典型的Java应用程序什么时候完成?
如果我在main方法中启动一个新线程,然后main方法结束,但另一个线程继续工作,应用程序仍然会打开,直到它的所有线程都已经死了,不是吗?
谢谢,圣诞快乐!
在Java中,你可以这样写:
public static void Main(String[] args) { }
Run Code Online (Sandbox Code Playgroud)
然后“右键单击”=>“运行为”=>“Java 应用程序”,它将运行当前的 Main 方法。
有机会在 Visual Web Developer 中实现这一点吗?
谢谢
鉴于以下课程,我试图找到程序的主要入口点:
public class Demo {
public static void main(String[] args) {
System.out.println("Hello World!");
}
public static void main(String a, String b){
// ...
}
public void main(int a){
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示感谢,谢谢.
这个问题可能是一个非常新手的问题,但这对我来说非常困惑.我正在研究Java网络,我很好奇这个main方法的后端.
public static void main(String[] args) throws IOException
Run Code Online (Sandbox Code Playgroud)
我理解它main启动一个线程?所以,即使我有一个简单的"Helloworld"程序,一个线程仍然存在,直到你关闭整个IDE或系统?
这是否意味着我可以在main方法中包含任何任意代码,我也想永远运行它(例如,心跳传感器检查或其他一些检查).
谢谢
我有两节课:
public class node {
static LinkedList<Integer> nodes = new LinkedList<Integer>();
public boolean visited;
public static void Main (String args []) {
System.out.println("Number of nodes in network");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i=1;i<=n;i++){
nodes.add(i);
}
System.out.println(nodes);
}
Run Code Online (Sandbox Code Playgroud)
和另一堂课
public class AdjacencyList {
private int n;
private int density;
Run Code Online (Sandbox Code Playgroud)
我想访问int n的Main方法,并将其值分配给private int n在AdjacencyList类.我尝试node.n(class.variable)格式的方法,我可以分配值,但它没有工作.有人可以帮帮我吗?
我已经使用 C# 一段时间了,但主要是在 Unity 中。我最近才开始在 Visual Studio 中简单地编写 C# 代码。
我只是在玩弄一个带有数组的队列,并对构造函数进行了一些研究。在我的 Queue 类中,我有一个为数组本身设置实例的构造函数:
public class Queue
{
int front = 0;
int rear = -1;
int size = 0;
const int maxSize = 5;
int[] queue;
public Queue()
{
queue = new int[maxSize];
}
//rest of class
}
Run Code Online (Sandbox Code Playgroud)
然后在调用的类中创建一个队列并使用它进行一些测试等,我使用了一个 main 方法:
class program
{
static void Main()
{
Queue myQueue = new Queue();
myQueue.enQueue(1);
myQueue.enQueue(2);
myQueue.enQueue(3);
myQueue.enQueue(4);
myQueue.enQueue(5);
myQueue.enQueue(6);
Console.WriteLine(myQueue.deQueue());
Console.WriteLine(myQueue.deQueue());
myQueue.enQueue(6);
myQueue.enQueue(7);
Console.WriteLine(myQueue.deQueue());
Console.WriteLine(myQueue.deQueue());
Console.WriteLine(myQueue.deQueue());
Console.WriteLine(myQueue.deQueue());
Console.WriteLine(myQueue.deQueue());
Console.WriteLine(myQueue.deQueue());
Console.ReadLine(); …Run Code Online (Sandbox Code Playgroud) 我正在创建一个步步高游戏.首先,我只是尝试放入一个main方法,以便我可以放入一些准备转换成真实的伪代码.然而,每当我尝试编译我现在的代码时,我得到一个编译器错误,声明:"类,接口或枚举期望"在主字符串中的'void'之前.
我是Java编程的新手,但有几周的经验.我的一些朋友也创建了一个,他们遇到了同样的问题.
我目前正在使用BlueJ作为我的开发环境.代码如下.
/**
* This is my Director class for my Backgammon game.
*
* @author ######
* @version 1.0
*/
public static final void main(String[] args)
public class Director
{
// instance variables - replace the example below with your own
/**
* Constructor for objects of class Director
*/
public Director()
{
// initialise instance variables
}
}
Run Code Online (Sandbox Code Playgroud) main-method ×13
java ×10
c# ×2
bluej ×1
class ×1
command-line ×1
constructor ×1
exception ×1
gradle ×1
inheritance ×1
lifecycle ×1
oop ×1
procedural ×1
runas ×1
testing ×1
unit-testing ×1
void ×1