【问题标题】:Calling different classes with an if statement in java?在java中使用if语句调用不同的类?
【发布时间】:2015-08-11 06:24:11
【问题描述】:

我正在编写一个 TBAP(文本基础冒险程序)只是因为。我刚开始,我已经遇到了问题。我想要做的是有一个在输出文本中介绍程序的主类。在课程结束时,它会问“你想去哪里冒险?”它有五个选项,其中 3 个是单独的冒险,其中两个是库存类。现在我被困在我的第一个冒险课上。我有一个名为 path 的 int 变量。如果路径== 1,你去幻想岛类继续你的冒险。有没有人可以用 if 语句来称呼这种冒险?我用我的变量名称和路径创建了一个构造函数、getter 和 setter。

暑期项目班:

package summerproject;


import java.util.Scanner;
import static summerproject.Fanastyisland.name;
import static summerproject.Fanastyisland.path;

public class Summerproject {
private static int path;
private static String name; 



public Summerproject (int path, String name)
{
    this.path = path;
    this.name = name;


}


public String getname() {
    return name;
}




public void setname(String name) {
    this.name = name;
}




public int getPath() {
    return path;
}


    public void setPath(int path) {
    this.path = path;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);

System.out.println("Welcome to the adventure text program! You are the choosen one to save the universe");



    System.out.println("Press any key to continue...");
    try
    {
        System.in.read();
    }  
    catch(Exception e)
    {}  

    System.out.println("Welcome. You are the choose one, a legend,a becon of      hope to save the universe from the forces of evil.");
    System.out.println("Only with you skills and your great power can you destroy the evil doing world.");
    System.out.println("Please enter heros name");
    name = in.next();

    System.out.println("Okay " + name + ", lets begin our adventure!!");

    System.out.println("The world can be saved, there is hope. But in order to save the world, \n "
            + "+ you must complete 9 tasks in three diffrent places in three diffrent periods of time. The past, the present and the future.");
     System.out.println("Press any key to continue...");
    try
    {
        System.in.read();
    }  
    catch(Exception e)
    {}  

    System.out.println("The three places are the past in the year 1322 in Fantasy island");
    System.out.println("The present is the evil little town of Keene N.H.");
    System.out.println("And the future to the year 2567 in Space!"); 



    System.out.println("Where would you like to go on your adventures?");
   System.out.println(" 1). Fantasy Island");
    System.out.println(" 2). Keene");
    System.out.println(" 3). Outer space");
    System.out.println(" 4). Buy wepons or potions!");
    System.out.println(" 5). Sell wepons!"); 
    path = in.nextInt();

   if (path == 1)
    {

    }


}
}

这是我的梦幻岛班:

package summerproject;


import java.util.Scanner;
import static summerproject.Fanastyisland.name;
import static summerproject.Fanastyisland.path;
 public class Fanastyisland extends Summerproject {
public static String name;
    public static int path;


    public Fanastyisland (String name, int path)
    {
        super(path,name);
        name = name;
        path = path;


    }
    public String getName() {
    return name;
}




public void setName(String name) {
    this.name = name;
}




public int getPath() {
    return path;
}


    public void setPath(int Path) {
    this.path = path;
}




public static void main(String[] args) 
//this is where the fantasy island        adventure begins. 
{
   System.out.println("Welcome to fantasy island!!")



}
}

就像我说的,我想用 if 语句调用子类,但我不知道该怎么做。如果我输入一个1,我想去幻想岛班。我还没有编写冒险程序,我会在修复后进行,我现在只想输出“欢迎来到幻想岛!”当我输入 1 时。任何帮助都会很棒!谢谢!

【问题讨论】:

  • 你为什么不创建一个带有每个冒险都会实现的 start 方法的通用接口,而不是让它扩展 Summerproject。然后你可以创建一个具有接口类型的类变量,并且每次都调用 start 方法。
  • 你想调用Fanastyisland.main(new String[0]);顺便说一句你想一次读取文本行,并且你永远不想忽略异常,除非你真的确定这是一个好主意。
  • @KevinEsche Goahead 并回答它。这是非常明显的解决方案 afaik。

标签: java class inheritance if-statement adventure


【解决方案1】:

类似这样的:

Summerproject adventure = null;
switch (path) {
  case 1:
    adventure = new FantasyIsland (...);
    break;
  case 2:
    adventure = new Keene (...);
    break;
  ...
  default:
    System.out.println ("Illegal choice(" & path & "): try again");
  }
  if (adventure != null) {
    adventure.play ();
    ...

【讨论】:

    【解决方案2】:

    你可以只创建一个通用接口

    public interface Adventures{
        public void start();
    }
    

    每个冒险都可以实现这个接口并覆盖start方法

    public class AdventureA implements Adventures {
    
        @Override
        public void start() {
            // Do whatever you want
        }
    
    }
    

    你的summerproject可以简单地拥有一个带有接口类型的类变量。

    public class Summerproject {
    private static int path;
    private static String name; 
    private Adventure adventure;
    ...
    }
    

    然后在 if 语句中,您可以分配这个冒险并调用 start 方法。

    if (path == 1)
    {
        adventure = new AdventureA();
        adventure.start();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 2017-01-26
      • 2013-05-30
      • 1970-01-01
      • 2023-04-04
      • 2020-01-08
      • 2021-07-19
      相关资源
      最近更新 更多