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

类在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级讲师

reply all(2)
迷茫

Isn’t the error message very detailed? Page cannot be directly converted to PagePlus.
Because a PagePlus is a Page, but a Page is not necessarily a PagePlus and cannot be downcast.
One solution: write a constructor for PagePlus to accept a Page,

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

Construct the page returned by articleDao.list() into a PagePlus and return

刘奇

According to JDK code

public class ClassCastException extends RuntimeException

It can be seen that ClassCastException is a subclass of RuntimeException

Your code

articleDao.list()

What should be returned should be a Long or a List of Articles, not PagePlus, so an exception will naturally be reported

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template