co*_*ndp 7 java android kotlin
在Java中,我可以声明这一点
private List a;
//onCreate
a = new ArrayList()
Run Code Online (Sandbox Code Playgroud)
但是在Kotlin中它显示错误,它迫使我特定一种类型
private List<String> a
Run Code Online (Sandbox Code Playgroud)
有时我不想提供类型(我不需要),但它在Kotlin中显示错误例如在Java中
public abstract class BaseActivity<T extends ViewDataBinding> extends AppCompatActivity {
//something
}
public abstract class BaseFragment {
private BaseActivity activity;
//something
}
//in kotkin I can't write
lateinit var activity: BaseAtivity //show error here (I have to specific a type but this is the base class and I do not want to specific a type here). I just want a reference of BaseActivity
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof BaseActivity) { //good in java but show error
//in kotlin because I have to //specific a type like BaseAtivity<something>
BaseActivity activity = (BaseActivity) context;
this.mActivity = activity;
activity.onFragmentAttached();
}
}
Run Code Online (Sandbox Code Playgroud)
我可以在Kotlin中编写什么来在java中实现相同的代码
| 归档时间: |
|
| 查看次数: |
1096 次 |
| 最近记录: |