我有这个方法:
public LeftDrawerView(Activity activity, Context context, LeftDrawerViewInterface delegate)
{
this.activity = activity;
this.context = context;
this.delegate = delegate;
}
要调用上述方法,我使用以下方法调用:
LeftDrawerView leftDrawerView new LeftDrawerView(this, this, this);
以后可能会混淆,我想简化一下,有没有办法做到这一点?
我可以这样调用各个变量:
LeftDrawerView leftDrawerView new LeftDrawerView();
leftDrawerView.context = this;
leftDrawerView.activity = this;
leftDrawerView.delegate = this;
这更具可读性,但需要做更多代码。我想避免这种情况。任何建议将不胜感激。
答案 0 :(得分:1)
您可以再创建一个构建器,它将调用您现有的构造函数:
public LeftDrawerView(Activity from) {
this(from, from, from);
}
答案 1 :(得分:0)
像这样传递CustomActivity会不会更容易:
public LeftDrawerView(CustomActivity customActivity){
this.customActivity = customActivity;
}
而且,如果你需要,只需这样投射:
((Activity) LeftDrawerView.this.customActivity);
((Context) LeftDrawerView.this.customActivity);
((LeftDrawerViewInterface) LeftDrawerView.this.customActivity);
答案 2 :(得分:0)
这样做:
public LeftDrawerView () {};
然后你会创建适当的setter:
public void setActivity(Activity activity) {...};
最后是适当的吸气剂:
public Activity getActivity() {...};
public Context getContext() {...};
public LeftDrawerViewInterface getLeftDrawerViewInterface() {...};
您可以使用单个属性存储Activity
实例,然后在每个getter上执行正确的转换。