选择题:
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());
}
}
}
1、在Java类的继承机制当中,子类可以继承父类的私有属性,但是必须在构造函数第一行写明super(),并且在构造函数的参数之中必须注明该参数;
2、在子类内部使用父类继承来的私有属性变量的时候,不能直接用this.xxx,而必须通过this.getXXX()来进行。
3.在进行函数调用的时候,程序根据传递的参数(类型,个数,顺序)来决定调用哪个函数