2013-04-09 52 views
0

1:多重構造和空值

Med.setQuiz(new Quiz(Cat, null)); 

測驗類:

public Quiz(final String category, TextView outputTxt) { 
     MyTxt = outputTxt; 
      StartAct(category); 
} 

2:從另一個類開始測驗類:

Quiz MyNewQ = new Quiz(null, Txt); 

從類開始測驗類首先:我開始上課,設置一些值與StartAct(category);

Then我再次使用了課程:Quiz(null,Txt);但是當我使用它,StartAct值設置爲空

但我想用我在第一類中設置值[測驗(貓空)]

我怎樣才能做到這一點?

回答

0

您可以使用這樣的靜態值:

public class Quiz{ 

    private static String category; 

    public Quiz(final String category, TextView outputTxt) { 

      // check category is not null or empty, don't set if it is 
      // which means that we must call the constructor with a valid category 
      // the first time or perhaps set a default value 
      if(category!=null && !category.equals("")){ 
       Quiz.category = category; 
      } 
      MyTxt = outputTxt; 
      StartAct(Quiz.category); 

    } 
} 
+0

不客氣。 – Simon 2013-04-10 05:43:11