【发布时间】:2015-02-28 14:19:51
【问题描述】:
您好,我想弄清楚下面的代码是如何工作的及其含义:
public Context mContext;
private Common mApp;
public Activity mActivity;
mContext = this;
mActivity = this;
mApp = (Common) mContext.getApplicationContext()
为了我调用一个方法,我需要创建该类的一个对象,如下所示
Context mContext = new Context();
and then i would be able to invoke the method getAppliationContext as shown above:
mContext.getApplicationContext()
有人可以为我解释一下吗,以及上面的 this 关键字是如何应用的。据我所知,this 关键字是对对象本身的引用。 我也不明白“mApp”是如何被用作一个对象来调用下面的各种方法的:
int startCount = mApp.getSharedPreferences().getInt("START_COUNT", 1);
mApp.getSharedPreferences().edit().putInt("START_COUNT", startCount+1).commit();
尚未使用 new 关键字创建它,我认为我需要执行以下操作才能使用 mApp 调用方法:
Common mApp = new Common();
这样我就可以做到了
int startCount = mApp.getSharedPreferences().getInt("START_COUNT", 1);
mApp.getSharedPreferences().edit().putInt("START_COUNT", startCount+1).commit();
我认为我的目标是,是否有其他方法可以在不使用 new 关键字的情况下创建对象。
【问题讨论】:
标签: android android-activity android-context