2016-06-07 28 views
0

我目前正在製作一個遊戲,發生在大四的10多天。我爲這款遊戲創建了一個介紹,但我不確定如何把它放在一起。我應該將介紹作爲父類,並且每天都有不同的子類?這是我的代碼到目前爲止。基於文本的遊戲與選擇,發生在多天

package SeniorGame; 

import java.util.Scanner; 

public class Intro{ 
    public static void main (String[]args){ 
    Scanner sc = new Scanner(System.in); 

    System.out.println("Before the game begins I need to know a few things about yourself."); 
    System.out.println(); 

    String name; 
    System.out.println("What is your name? "); 
    name=sc.next(); 

    String crush; 
    System.out.println("Who was your High School crush? "); 
    crush=sc.next(); 

    String bfriend; 
    System.out.println("Who was your best friend in High School? "); 
    bfriend=sc.next(); 

    System.out.println(); 
    System.out.println(); 

    System.out.println("This is a story that consits of 12 days in your senior year of High School.\n" 
      + "The days are spread out from the beginning to the end of the year.\nThe choices you make" 
      + " will impact the way your story plays out."); 
} 

}

+0

'extends'提煉行爲並用於「is-a」關係(香蕉是一種水果)。與你的介紹有關係嗎? – zapl

+0

從技術上講,介紹不是一個獨立的東西。如果這是有道理的。從介紹開始,你將被提升爲遊戲菜單,以播放或退出。如果你退出system.quit(1)行觸發器,但如果不行,你顯然會玩遊戲。我的問題是如何從這裏開始第一天。我是否以單獨的課程或單獨的方法進行第一天課程? –

+0

我會讓每一天繼承Day類。讓外部的課程處理日常移動的邏輯,記錄日期可能會分享的變量等。 –

回答

0

如果以同樣的方式你的日子進步,也有類似的行爲,你可能會考慮定義SchoolDay接口,然後創建一兩天爲實現此接口的類。主要Intro課程中的變量可以跟蹤玩家在整個過程中所做的事情。

下面是一個例子。在這裏,我已經決定,保持做功課的軌道是對比賽的勝負至關重要的(我無聊):

public interface SchoolDay { 
    public void doMorningClass(); 
    public void doLunchTime(); 
    public void doAfternoonClass(); 
    public boolean doHomework(); //Sets a boolean to true, see below 
} 

然後,在你的主類(我將重新命名,說實話):

public class SchoolGame { 
    private static boolean[] homeworkDone; 
    //Your other global variables such as crush and name go here 
    //Make getters and setters for all of these so the day objects can 
    //access and change them. 

    public static void main(String[] args) { 
     homeworkDone = new boolean[12]; //One boolean for each day 

     //Move your intro stuff into a method and call it here 
     //The main method continues once they fill out the info 

     //Instantiate day objects and run their school time methods 
     //If the player decides to do homework during those times, for example 
     //Then that day's doHomework() method is called 
     //Which would affect the boolean of that day under some condition 
     ... 

此設置可能不完全適合您的需求,因爲您的描述有些模糊,並且此處沒有具體問題。如果你認爲你的day對象需要所有日子都有的變量,那麼使用父類(可能是抽象的)而不是接口。

+0

這實際上確實對你很有幫助。 –