gpt4 book ai didi

java - Android 中的类就像 Swift 中的 Struct

转载 作者:行者123 更新时间:2023-11-29 20:23:28 25 4
gpt4 key购买 nike

在我的 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;
}
}

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

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

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

最佳答案

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..
}

关于java - Android 中的类就像 Swift 中的 Struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32855751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com