Android中的类,如Swift中的Struct

时间:2015-09-30 00:12:36

标签: java android

在swift的应用程序中我有这段代码:

struct Question
{
    var QuestionLbl : String!
    var Answers : [String]!
    var Answer : Int!
}

之后我像这样使用这个结构

var Questions = [Question]()

Questions =
            [Question(QuestionLbl: "Whats my name?", Answers: ["John","Josh","Adam","Leo"], Answer: 0),

                Question(QuestionLbl: "Whats my moms name?", Answers: ["Jessica","Crystal","Samanta","Kate"], Answer: 3),

                Question(QuestionLbl: "Whats my fathers name?", Answers: ["Ed","Blake","Jeff","Jonhson"], Answer: 2)]

现在,我正在尝试为Android制作相同的应用.. 所以我创建了一个类,并尝试做同样的事情.. 这是类文件:

public class Question {
    String questionLabel;
    String[] answersOptions;
    Integer correctAnswer;

    public Question(String questionLabel, String[] answersOptions, Integer correctAnswer) {
        this.questionLabel = questionLabel;
        this.answersOptions = answersOptions;
        this.correctAnswer = correctAnswer;
    }

    public String getquestionLabel() {
        return questionLabel;
    }

    public String[] getanswersOptions() {
        return answersOptions;
    }

    public Integer getcorrectAnswer() {
        return correctAnswer;
    }
}

这就是我在主要活动中尝试做的事情:

Question[] questions;
        questions = {
                 Question("Whats my name?",{"John","Josh","Adam","Leo"}, 1),
                 Question("Whats my mom's name?",{"Jessica","Crystal","Samanta","Kate"}, 1)
        };

但它没有用。 什么是问题?

1 个答案:

答案 0 :(得分:2)

查看this帖子,了解声明数组的语法。从本质上讲,它看起来像:

questions = new Question[]{
    new Question("What's my name?", new String[]{"John","Josh","Adam","Leo"}, 1),
    new Question("What's my mom's name?", new String[]{"Jessica","Crystal","Samanta","Kate"}, 1)
    //etc..
}