【问题标题】:meaning of "return this"“退还这个”是什么意思
【发布时间】:2013-06-15 19:55:30
【问题描述】:

我一直在关注互联网上关于 SQLite 的 android 教程。我有一个我不确定的声明:

public HotOrNot open(){
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;  
}

return this 在这个方法中是什么意思?它是当前对象还是上下文?谢谢

【问题讨论】:

  • 调用此方法的HotOrNot 的当前实例。
  • ... the keyword this denotes a value that is a reference to the object for which the instance method was invoked. JLS 15.8.3
  • 也就是说,当在对象上调用方法(函数)时,this指的是调用当前方法的对象。

标签: java android methods return


【解决方案1】:

return this 返回当前对象实例。我不知道HotOrNot 类是什么,但很明显这个方法是在那个类中定义的。在该方法中,成员变量 ourHelper 使用新的数据库帮助程序进行初始化,并且成员变量 ourDatabase 被分配了来自该帮助程序的可写数据库对象 - 之后返回类的实例 - 很可能,以便调用可以被锁住,例如

new HotOrNot(myContext).open().runQuery("some query text")

有关this 关键字的更多信息,请参阅Java language specifications

【讨论】:

    猜你喜欢
    • 2011-04-11
    • 2018-10-29
    • 1970-01-01
    • 2017-05-25
    • 2010-10-03
    • 2013-02-21
    • 2015-01-09
    • 2011-10-12
    • 2015-06-22
    相关资源
    最近更新 更多