1

我正在创建一个模型类,假设它被称为“历史”,它将与 SQLite 数据库交互。

我们是否总是需要为模型类手动创建 getter/setter/fields?没有自动生成器吗?

模型类示例:

package com.example.fileexplorermanager;


public class History {

    //private variables
    int _id;
    String _file_name;
    String _full_path;
    String _file_type;

    // Empty constructor
    public History(){

    }
    // constructor
    public History(int id, String _name, String _full_path, String _file_type){
        this._id = id;
        this._file_name = _name;
        this._full_path = _full_path;
        this._file_type = _file_type;

    }

    // getting ID
    public int getID(){
        return this._id;
    }

    // setting id
    public void setID(int id){
        this._id = id;
    }

    public String getFileName(){
        return this._file_name;
    }

    public void setFileName(String file_name){
        this._file_name = file_name;
    }

    public String getFullPath(){
        return this._full_path;
    }

    public void setFullPath(String full_path){
        this._full_path = full_path;
    }

    public String getFileType(){
        return this._file_type;
    }

    public void setFileType(String file_type){
        this._file_type = file_type;
    }

}
4

4 回答 4

5

如果你使用 Eclipse:Source -> Generate Getters and Setters...

于 2013-01-08T00:28:33.643 回答
5

更新

从 Android Studio v3.0.1 开始:

在 Android Studio 中,按ALT+ INSERT(或⌘</kbd> + N for MacOS), you will have following choices (including your solution!):

  • 构造函数
  • 吸气剂
  • 二传手
  • Getter 和 Setter
  • equals() 和 hashCode()
  • toString()
  • 覆盖方法...
  • 实现方法...
  • 委托方法...
  • 超级方法调用(在覆盖方法内部时)
  • 版权
  • App Indexing API 代码(在扩展Fragment的类中不可用。)

选择所需的选项并选择方法。完毕!

于 2015-09-24T09:53:32.063 回答
3

如果您在 Eclipse 上,请右键单击源文件 -> 源 -> 生成 Getter 和 Setter...在此处输入图像描述

于 2013-01-08T00:28:30.897 回答
0

Android没有提供任何东西,但是有像ORMLite这样的第三方库。

于 2013-01-08T00:28:51.433 回答