简体   繁体   English

如何在一堂课中执行不同的课

[英]How to execute different classes in one class

I want to create a program that can do all the stuff from another code, depending on user input. 我想创建一个程序,该程序可以根据用户输入执行其他代码中的所有操作。 Something like this: 像这样:

import java.util.Scanner;
public class Main_Programm1 {
    public static void main(String args[]) {
        String something = "something";
        String something2 = "something2";
        Scanner userInput = new Scanner(System.in);
        String action = userInput.next();
        if (action.equals(something)) {
            //here i want to execute all the code from class Main_Programm2
        } else if (action.equals(something2)) {
            //here i want to execute all the code from class Main_Programm3 and so on
        }
    }
}

How do i do it? 我该怎么做?

Actually, you've got it all done, only creates the Objects that you need ;-) 实际上,您已经完成了所有工作,仅创建所需的对象;-)

import java.util.Scanner;
// imports classes;

public class Main_Programm1 
{
  public static void main(String args[]) 
  {
    String something = "something";
    String something2 = "something2";
    Main_Programm main_prog;
    Main_Programm2 main_prog2;

    Scanner userInput = new Scanner(System.in);
    String action = userInput.next();
    if (action.equals(something)) 
    {
      main_prog = new Main_Programm();
      //.....
    } 
    else if (action.equals(something2)) 
    {
      main_prog2 = new Main_Programm2();
      //.....
    }
  }
}

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

相关问题 如何使用不同的类对象创建一个类的数组? 爪哇 - how to create an array of one class using a different classes objects? Java 如何在JGRASP编辑器中的一个类中导入不同的类 - How to import different classes in one class in JGRASP editor 如何避免模拟注入在一个测试类中对不同类的影响? - How to avoid impact of mock injection in one test class to different classes? 如何将不同的班级用作一个班级的成员? - How do I use different classes as member for one class? 如何将值从一个类发送到不同的类(多个类) - how to send value from one class into different classes(more than one class) 一个类如何用两个不同的类扩展当一个类来自外部库而另一个是内置的时 - How a class can extend with two different classes While one class is from a external library and other is built in 如何添加两个不同类的ArrayList,将一个抽象类扩展为一个ArrayList? - How do I add two ArrayLists of different classes that extend an abstract class into one ArrayList? 如何获得泛型? class 来自 json 回应? 如果一个object可以包含很多不同的类 - How to get generic? class from json response? If one object can contain many different classes 如何在2个不同的类上使用一个数组 - How to use one array on 2 different classes 如何为不同的类编写一个接口实现? - How to write one interface implementation for different classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM