java中给类增加属性的问题
PHP中文网
PHP中文网 2017-04-17 12:01:17
0
2
683

类在jar包里,不方便直接修改类.. 想自定义一个类继承那个类 增加属性:

    public class Page<E> extends ArrayList<E> {
    /**
     * 不进行count查询
     */
    private static final int NO_SQL_COUNT = -1;

    /**
     * 进行count查询
     */
    private static final int SQL_COUNT = 0;

    private int pageNum;
    private int pageSize;
    private int startRow;
    private int endRow;
    private long total;
    private int pages;

    public Page(int pageNum, int pageSize) {
        this(pageNum, pageSize, SQL_COUNT);
    }

    public Page(int pageNum, int pageSize, boolean count) {
        this(pageNum, pageSize, count ? Page.SQL_COUNT : Page.NO_SQL_COUNT);
    }

    public Page(int pageNum, int pageSize, int total) {
        super(pageSize > -1 ? pageSize : 0);
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total;
        this.startRow = pageNum > 0 ? (pageNum - 1) * pageSize : 0;
        this.endRow = pageNum * pageSize;
    }

    public Page(RowBounds rowBounds, boolean count) {
        this(rowBounds, count ? Page.SQL_COUNT : Page.NO_SQL_COUNT);
    }


    public Page(RowBounds rowBounds, int total) {
        super(rowBounds.getLimit() > -1 ? rowBounds.getLimit() : 0);
        this.pageSize = rowBounds.getLimit();
        this.startRow = rowBounds.getOffset();
        //RowBounds方式默认不求count总数,如果想求count,可以修改这里为SQL_COUNT
        this.total = total;
        this.endRow = this.startRow + this.pageSize;
    }

    public List<E> getResult() {
        return this;
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public int getEndRow() {
        return endRow;
    }

    public void setEndRow(int endRow) {
        this.endRow = endRow;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
        if (this.pageSize > 0) {
            this.pages = (int) (total / this.pageSize + ((total % this.pageSize == 0) ? 0 : 1));
        } else {
            this.pages = (int) total;
        }
    }

    public boolean isCount() {
        return this.total > NO_SQL_COUNT;
    }

    @Override
    public String toString() {
        return "Page{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                ", startRow=" + startRow +
                ", endRow=" + endRow +
                ", total=" + total +
                ", pages=" + pages +
                '}';
    }
}

这是一个分页类,我想增加两个属性:

private int nextPage;
private int previousPage;

自定义一个类来继承:

public class PagePlus<E> extends Page<E> {
    private int nextPage;
    private int previousPage;

    public PagePlus(int pageNum, int pageSize) {
        super(pageNum, pageSize);
        this.nextPage = super.getPageNum() + 1;
        this.previousPage = super.getPageNum() - 1;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    public int getPreviousPage() {
        return previousPage;
    }

    public void setPreviousPage(int previousPage) {
        this.previousPage = previousPage;
    }
}

程序中是这样写的:

 @Override
    public PagePlus<Article> list(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PagePlus<Article> page = (PagePlus<Article>) articleDao.list();
        return page;
    }

articleDao.list 返回的是List

java.lang.ClassCastException: com.github.pagehelper.Page cannot be cast to util.PagePlus

请问是哪里写的有问题?

PHP中文网
PHP中文网

认证0级讲师

répondre à tous(2)
迷茫

报错的信息不是说的很详细吗,Page不能直接转化为PagePlus.
因为一个PagePlus是一个Page,但一个Page不一定是一个PagePlus,不能downcast.
解决的方法之一:给PagePlus写一个构造函数,接受一个Page,

public PagePlus(Page page) {
    this(page.getPageNum(), page.getPageSize());
    this.pages = page.getPages();
    //... 省略
}

articleDao.list()返回的page构造为一个PagePlus再返回

刘奇

根据JDK的代码

public class ClassCastException extends RuntimeException

可知ClassCastException是RuntimeException的子类

你的代码

articleDao.list()

返回的应该是Long或者是Article的List,而非PagePlus,自然会报异常

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal