简体   繁体   English

是否可以从已经具有main方法的类中调用main方法?

[英]Is it possible to call to main method from a class that already has a main method?

I have a program with two class and each has a main method, i wanted to know if it is possible to call the main method from my second class to work in my first class. 我有一个包含两个类的程序,每个类都有一个main方法,我想知道是否可以从第二个类中调用main方法来在第一个类中工作。 I can't seem to find any examples that will help, that makes me think that it's not possible for me to do what I want to. 我似乎找不到任何有帮助的例子,这使我认为我不可能做自己想做的事。

First Class: 头等舱:

    package scannerarrays;

        import java.util.Scanner;

             public class ScannerArrays {



public static void main(String[] args) {
    Scanner input = new Scanner (System.in);
    String words;
    int IDnum;


    System.out.println("Enter your Name");
    words = input.nextLine();

    System.out.println("Enter your Surname");
    words = input.nextLine();

    System.out.println("Enter your ID number");
    IDnum = input.nextInt(); 

Second Class: 二等舱:

 package scannerarrays;

import java.util.Scanner;

public class IdDetails {


String id;
int month[] = {31 , 29 , 31 , 30 , 31, 31 , 30 , 31 , 30 , 31};


public IdDetails()  {


    Scanner input = new Scanner(System.in);
    System.out.println("Enter your ID number \nLike : 000000000V");
    id = input.next();



     }

public int getYear() {
    return(1900 + Integer.parseInt(id.substring(0, 2)));

}
    public int getDays() {
     int d = Integer.parseInt(id.substring(2, 5)); 
     if (d > 500) {
         return (d - 500);
     }else{
         return d;



     }

    }
public void setMonth() {

    int mo = 0, da = 0;
    int days = getDays();

    for (int i = 0; i < month.length; i++) {
        if (days < month[i]) {
            mo = i + 1;
            da = days;
            break;

        }else{
            days = days - month[i];
        }        

        }

  System.out.println("Month: " + mo + "\nDate : " + da);

    }

    public String getSex() {
        String M = "Male" , F = "Female";
        int d = Integer.parseInt(id.substring(2 , 5));
        if (d>81) {
            return F;

        }else{
         return M;   
        }

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

IdDetails ID = new IdDetails();
System.out.println("Your Details of DOB from ID");
System.out.println("Year : " + ID.getYear());
ID.setMonth();
System.out.println("Sex : " + ID.getSex());

      }



     }

The main method is like any other static method of your class, so you can call it the same way, just like: main方法与类中的任何其他静态方法一样,因此您可以用相同的方式调用它,就像:

IdDetails.main();

Or with any number of String parameters: 或使用任意数量的String参数:

IdDetails.main("name", "surname", "12");

But that seems a litte bit confusing, to use a logic within a main method this way. 但这似乎有点混乱,以这种方式在主要方法中使用逻辑。 If you really need to do it, just make another method with fixed input parameters and call it everywhere you need it (in your case in both main methods). 如果确实需要执行此操作,则只需创建另一个具有固定输入参数的方法,然后在所需的任何地方调用它(在两种情况下,这都是主要方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM