2012-05-27 3 views
1

목록 객관식 질문 목록을 표시하는 프로그램을 작성하고 있습니다. 내 Java 클래스의 ChoiceString 데이터 멤버에 저장된 값에 문제가 있습니다. 클래스의 각 인스턴스에는 ChoiceString 자체의 값이 있어야하지만 어떤 이유로 ChoiceString은 첫 번째 인스턴스에 의해 초기화 된 값만 보유하고 다른 인스턴스와 공유합니다. 각 인스턴스는 고유 한 ChoiceString 고유 값을 갖기를 원합니다. 어떻게 해결할 수 있습니까? 클래스 인스턴스가 데이터 멤버의 값을 공유합니다.

다음

내 코드입니다 : 이것은 내 인스턴스에 대한 코드입니다

public class AllChoiceQuestion extends ChoiceQuestion{ 

    private String note = "Note: The following question has one or more correct choices\nYou must give the number(s) of ALL correct anwers, in order,\nto get full credit andsperated by spaces"; 
    private int count =0; 
     // A varabile that will hold the user answer 
    private String choiceString; 


public AllChoiceQuestion(String questionText) { 
    // initilize the question text value 
      super(questionText); 
    choiceString=""; 

} 

    public void addChoice(String choice, boolean correct){ 
     super.addChoice(choice, correct); 
     if(correct == true){ 
      count++; 
      choiceString += "" + count+" " ; 
      } 
     super.setAnswer(choiceString.trim()); 
     } 

    public void display(){ 
     System.out.println(note); 
     super.display(); 
    } 

    public String toString(){ 
     return note +"\n"+ super.toString(); 

    } 
} 

 ChoiceQuestion allchoicequestion1 = new AllChoiceQuestion("Which of the basic data type(s) has the size of 16 bit?"); 
     allchoicequestion1.addChoice("Char", true); 
     allchoicequestion1.addChoice("Short", true); 
     allchoicequestion1.addChoice("Float", false); 
     allchoicequestion1.addChoice("Double", false); 


     ChoiceQuestion allchoicequestion2 = new AllChoiceQuestion("Which of the basic data type(s) has the size of 64 bit?"); 
     allchoicequestion2.addChoice("Long", false); 
     allchoicequestion2.addChoice("Doulbe", false); 
     allchoicequestion2.addChoice("Byte", true); 
     allchoicequestion2.addChoice("Int", true); 

allChoiceQuestion1에 대한 그래서 ChoiceStrng 1 2 해야하며 ChoiceString forallChoiceQuestion2이 있어야 할 3 4 하지만, 매번 tyee 3 4 forallChoiceQuestion2에 대한 답변으로 나에게 틀린 대답을 준다. 그러나 1 2를 입력하면 정확 할 것이다.

+1

'AllChoiceQuestion' 클래스는 괜찮아 보입니다. 문제가 발생한 인스턴스를 생성하기위한 코드를 제공 할 수 있습니까? – Newbie

+0

내 게시물을 편집하고 하단에 코드를 추가했습니다. – nj2012

+0

오, 알았어. 지금 고맙다. – nj2012

답변

0

choiceString은 항상 생성자에서 초기화됩니다.

당신은 AllChoiceQuestion를 확장 할 필요가

choiceString = questionText; 
+0

사용자가 나중에 choiceString에 저장 될 값을 입력하기 때문에 ChoiceString의 값을 비워 두어야합니다. questionText는 클래스의 인스턴스를 만들 때 프로그래머가 초기화 할 문자열을 보유합니다. 그래서, 그들은 서로 배정되지 않습니다. – nj2012

+0

... 내 대답을 하향 회선으로 표현할 수 있다면. 나는 바르게 읽지 않았다. – user1320716

+1

그것에 대해 걱정하지 마라. – nj2012

0

을 시도하고 choiceString 자신의 구현을해야합니다 NumberChoiceQuestionStringChoicQuestion 같은 자식 클래스의이, 당신은 자식 클래스에서 choiceString 구현에 과부하가 아이의 객체를 생성해야 클래스는, 이런 식으로 특정 아이 클래스있을 것 choiceString


public class NumberChoiceQuestion extends AllChoiceQuestion{ 
     this.choiceString; // Over written value of choiceString specific to NumberChoiceQuestion 
    } 

public class StringChoiceQuestion extends AllChoiceQuestion{ 
     this.choiceString; // Over written value of choiceString specific to StringChoiceQuestion 
    } 
+0

ChoiceString은 AllChoiceQuestion 클래스의 멤버 일 뿐이며, AllChoiceQuestion 인스턴스를 만들고있다. 따라서 AllChoiceQuestion을 확장하는 하위 클래스는 없습니다. 문제는 AllChoice의 인스턴스를 만들 때마다 ChoiceString의 값이 모든 인스턴스에서 공유된다는 것입니다. 각 인스턴스에 ChoiceStrings의 자체 복사본이 있어야합니다. – nj2012

0

변수 correct이 false 일 때 문제가 증가하지 않습니다. 그러므로 선택 문자열은 항상 1입니다. 2.