用java中的if语句调用不同的类?

时间:2015-08-11 06:24:12

标签: java class inheritance if-statement adventure

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

Summerproject课程:

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,我想去幻想岛级。我还没有对冒险进行编程,一旦修复完毕,我就会对它进行编程,我只想让现在的输出为#34;欢迎来到幻想岛!"当我输入1时。任何帮助都会很棒!谢谢!

2 个答案:

答案 0 :(得分: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 ();
    ...

答案 1 :(得分:1)

您可以创建一个通用界面

public interface Adventures{
    public void 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();
}