- 最近在学习Kotlin过程中,无意间发现这个问题。当我们重写父类方法时编译器报错“overrides nothings”

- 本身这个类是直接由AS工具直接java转过来,按说应该没有什么大问题,通过stackoverflow查看后有人说跟kotlin的 “?”有关,于是有了一些思路,先查看其父类
public abstract class LetvBaseFragment extends Fragment implements LetvFragmentListener {
protected Context mContext;
protected View mContentView;
.......
}
- 其父类并没有重写该方法,继续查看SDK中Fragment类,顿时茅塞顿开:)
@Nullable
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return null;
}
看到其中对参数的定义,是否已经有了思路呢?
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState:
Bundle?): View?
{
mContentLayout = inflater!!.inflate(R.layout.fragment_huya_subscribe, null, true)
return mContentLayout
}
- 总结在java中其实我们并不关心重写时参数是否为空的判断,但在kotlin中重写时,一定需要对参数是否为空做相应的声明,否则就会报overides nothings错误。
网友评论