Java 类,类似于Swift中的结构
在我的swift应用程序中,我有以下代码:Java 类,类似于Swift中的结构,java,android,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","J
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)
};
但它不起作用。
问题出在哪里?请查看post,了解声明数组的语法。基本上,它看起来像:
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..
}
您需要使用
new
关键字。您必须使用new关键字来执行此操作。