Java-在另一个类中使用包的main方法

时间:2018-08-07 14:21:08

标签: java import package main

假设我有一个名为com.Gazzali的程序包,并且在此程序包中还有3个其他类。

package com.Gazzali;
//Driver class
public class Main {

public static void main(String[] args) {
    System.out.println("Hey There !");
    FireCall target = new FireCall(); // calls 2nd class (named: Firecall)
    target.callfired();
 }
}

二等班:

package com.Gazzali;

public class FireCall {
   public void callfired()
   {
    System.out.println("Calling function Triggered.");
    Execute Fire = new Execute(); //calls 3rd class (named : Execution).
    if(Fire.click() == 1)
        System.out.println("You're Dead, Boy !!!");
    else
        System.out.println("Whoooss Saved !!!");
    }
 }

三等舱:

package com.Gazzali;
import java.util.Scanner;

public class Execute {
int choice;
Scanner query = new Scanner(System.in);

public int click()
{
    System.out.println("Enter a choice : ");
    choice = query.nextInt();
    if(choice % 2 == 0)
    {
        return 1;
    }
    else
        return 0;
}
}

这3个包含我的com.Gazzali软件包。现在,在另一个文件(RunPackTest.java中,我想调用main类(Driver类)的Main方法。所以我尝试像下面这样导入:

import com.Gazzali.Main;
public class RunPackTest {
public static void main(String[] args) {
    Main run = new Main(); //calling Main method of Driver class of the package 
    System.out.println(run); //Doesn't seem to work,IDE only return 0 
}

}

如何执行此操作?因为main类的Main方法将启动程序并相应地调用程序包的其他类。

1 个答案:

答案 0 :(得分:1)

我相信这可以做到:

main

您可以像其他方法一样调用{{1}},尽管通常这不是一个好习惯。