gpt4 book ai didi

java - 如何在 Kotlin 中声明原始类型?

转载 作者:IT老高 更新时间:2023-10-28 13:42:05 24 4
gpt4 key购买 nike

在 Java 中我可以声明这个

private List a;

//onCreate
a = new ArrayList()

但在 Kotlin 中它显示错误,它迫使我指定一个类型

private List<String> a

有时我不想提供类型(我不需要),但它在 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();
}
}

我可以在 Kotlin 中编写什么来在 java 中实现相同的代码

最佳答案

您可以使用 star projections

例如

private List<*> a

关于java - 如何在 Kotlin 中声明原始类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com