创建一个Java模型类

时间:2018-07-19 19:49:47

标签: java android class

您好,我是Android开发的新手,我想为Declare @MyTableVar table (User_ID VARCHAR(32) primary key) INSERT @MyTableVar VALUES ('user3') INSERT @MyTableVar VALUES ('user5') INSERT @MyTableVar VALUES ('user10') INSERT @MyTableVar VALUES ('user999') INSERT @MyTableVar VALUES ('user2000') SELECT * FROM @MyTableVar WHERE User_ID NOT IN (SELECT USER_ID FROM database.schema.table_name) 的{​​{1}}任务创建一个AND ( ($x is not null and R.column = 2) OR ($x is null and R.another_column = 3) ) ,我在model,但在object中,我对所有的获取器,设置器等感到困惑。

这是我想用Java实现的我的Swift代码的一部分。

  

Task.swift

JSON

我想按上述方式处理输出,检查是否为Swift,然后返回空的Java或如果为class Task{ // from the Task var _id: String! var _title: String! var id: String{ if _id == nil{ _id = "" } return _id } var title: String{ if _title == nil{ _title = "" } return _title } init(taskDict: [String: AnyObject]){ // Task if let taskObject = taskDict["task"] as? [String: AnyObject]{ if let id = taskObject["_id"] as? String{ self._id = id } if let title = taskObject["title"] as? String{ self._title = title } } } 则返回0。

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

如果您是Android的新手,并且知道敏捷,那么应该尝试一下Kotlin,因为它非常相似。

在Kotlin中,您的代码可能类似于:

class Task (taskDict: HashMap<String, Any>) {

    var id: String = ""
    var title: String = ""

    init {
        // Task
        val taskObject = taskDict["task"] as HashMap<String, Any>?

        if (taskObject != null) {
            val id = taskObject["_id"] as String? ?: ""
            val title = taskObject["_object"] as String? ?: ""

            this.id = id
            this.title = title
        }

    }
}

答案 1 :(得分:1)

您的Java Task类如下所示:

 public class Task {

        private String id;
        private String title;

        public Task(String id, String title) {
            this.id = id;
            this.title = title;
        }

        public String getId() {
            if (id == null) {
                return "";
            }
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getTitle() {
            if (title == null) {
                return "";
            }
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }

答案 2 :(得分:1)

这是在Android Studio中完成的方式,或者我相信任何其他IDE:

1。创建一个新类:(右键单击包->新建-> Java类
2.给班级命名 创建实例:

private class Task {

//Instantiate your global variables
private String id;
private String title;

}

3。右键单击您的类文件->生成->吸气剂和设置器->选择您想要吸气剂和设置器的变量,仅此而已!

您的课程应如下所示:

public class Task {

    //Instantiate your global variables
    private String id;
    private String title;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}