qq_35071078
下课睡觉
2017-07-19 04:45

关于java abstract 的问题

  • java
  • android
  • 继承
  • abstract
  • override

我自己定义了一个类继承了RecyclerView.Adapter

//这个是我自定义的类,用来继承RecyclerView.Adapter
 public abstract class TableAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter {

}
//这个是我继承的类
 public abstract static class Adapter<VH extends ViewHolder> {
                    //这个类主要就这两个abstract方法
        public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);

        public abstract void onBindViewHolder(VH holder, int position);

然后我用一个类来继承我自定义的这个Adapter的话:

     class MyAdapter extends TableAdapter<MyAdapter.ViewHolder> {

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }

        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 0;
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            TextView name;
            TextView kill;
            TextView dead;

            public ViewHolder(View itemView) {
                super(itemView);
                name = (TextView) f(R.id.name);
                kill = (TextView) f(R.id.kill);
                dead = (TextView) f(R.id.dead);
            }

            private View f(int id) {
                return findViewById(id);
            }
        }

    }

就会报错:
图片说明

但是如果我直接继承RecyclerView.Adapter,就没问题:

     class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }

        @Override
        public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 0;
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            TextView name;
            TextView kill;
            TextView dead;

            public ViewHolder(View itemView) {
                super(itemView);
                name = (TextView) f(R.id.name);
                kill = (TextView) f(R.id.kill);
                dead = (TextView) f(R.id.dead);
            }

            private View f(int id) {
                return findViewById(id);
            }
        }

    }

请问这哪里有问题吗?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答