选择题:

package xuanzeti;

//定义选项类

public class Option {

private char id;//A,B,C,D选项

private String text;//选项内容

public Option(){

}

public Option(char id, String text){

this.id = id;

this.text = text;

}

public String getText(){

return text;

}

private void setText(String text) {

this.text = text;

}

public char getId(){

return id;

}

public void setId(char id){

this.id = id;

}

}

package xuanzeti;

public class Question {

    private int id;   //题号

    private String text; //题干内容

    public Question(){

    }

    public Question(int id, String text){

       this.id = id;

       this.text = text;

    }

    public void setId(int id){

       this.id = id;

    }

    public int getId(){

       return id;

    }

    public void setText(String text){

       this.text = text;

    }

    public String getText(){

       return text;

    }

    public void show(){

    }

    public boolean check(char[] answer){

       return false;

    }

}

package xuanzeti;

public class SingleQuestion

    extends Question{

    //选项

    private Option[]options;

    //标准答案

    private char answer;

    public SingleQuestion(){

    }

    //super()时候,参数列表中必须列出

    public SingleQuestion(int id,String text,

           Option[]options,char answer){//参数列表中必须标明从父类继承的属性

       super(id,text);//必须放在构造函数的第一行

       this.options = options;

       this.answer = answer;

      

    }

    public char getAnswer(){

       return answer;

    }

    public void setAnswer(char answer){

       this.answer = answer;

    }

   

    public Option[] getOptions(){

       return options;

    }

    public void setOptions(Option[] options){

       this.options = options;

    }

   

    public void show(){

       System.out.println(this.getId()+","+this.getText()+"()");            

       for(int i=0; i<this.options.length;i++){

           //Option o = options[i];

           System.out.println(this.options[i].getId()+""+

                  this.options[i].getText()+"\t");

       }

    }

   public boolean check(char s){

       return s == this.answer;

    }

}

package xuanzeti;

 

import java.util.Arrays;

 

public class MultiQuestions extends Question {

private char[] answers;//多项选择的标准答案

private Option[]options;

public MultiQuestions() {

 

}

//this.options代表的是private中的options

public MultiQuestions(int id,String text,

Option[]options,char[] answers){

super(id, text);

this.options = options;

this.answers = answers;

}

public void setAnswers(char[] answers){

this.answers = answers;

}

public char[] getAnswers(){

return answers;

}

public void setOptions(Option []options){

this.options = options;

}

public Option[] getOptions(){

return options;

}

public boolean check(char[] s){

return Arrays.equals(this.answers, s);

}

public void show(){

System.out.println(this.getId()+","+this.getText()+"()");

for(int i=0;i<this.options.length;i++){

System.out.println(this.options[i].getId()+","+

this.options[i].getText());

}

}

 

}

package xuanzeti;
 
import java.util.Scanner;
 
public class QustionDemo {
public static void main(String[] args) {
// 通过对象数组,多项选择和单选都是从Question继承过来的,可以作为对象数组的原素,类型都相同
Question[] que = new Question[2];
// 初始化单项选择
que[0] = new SingleQuestion(1, "下面那一个是赵本山的徒弟?", new Option[] {
new Option('A', "倪萍"), new Option('B', "小沈阳"),
new Option('C', "曾海波"), new Option('D', "王雪超"), }, 'B');
 
// 初始化多项选择题
que[1] = new MultiQuestions(2, "下面那些人物是西游记中的?", new Option[] {
new Option('A', "孙悟空"), new Option('B', "唐僧"),
new Option('C', "猪八戒"), new Option('D', "由志杰") }, 
new char[] {'A', 'B', 'C' });
Scanner console = new Scanner(System.in);
 
for (int i = 0; i < que.length; i++) {
// 显示问题
que[i].show();
// 读取答案
System.out.println("请输入答案");
char[] s = console.nextLine().toCharArray();
// 比较 输入答案与标准答案
boolean pass = que[i].check(s);
 
// 输出判断答题的结果
if (pass) {
System.out.print("你太牛叉啦!");
} else {
System.out.println("你需要更加努力啊0_n_0");
}
 
}
 
}
}
小结:

 1、在Java类的继承机制当中,子类可以继承父类的私有属性,但是必须在构造函数第一行写明super(),并且在构造函数的参数之中必须注明该参数;

2、在子类内部使用父类继承来的私有属性变量的时候,不能直接用this.xxx,而必须通过this.getXXX()来进行。

3.在进行函数调用的时候,程序根据传递的参数(类型,个数,顺序)来决定调用哪个函数