Page.java
package com.ufida.console.common.service;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.sitechasia.webx.core.exception.BaseUnCheckedException;
@SuppressWarnings("unchecked")
public class Page implements Serializable {
private static final long serialVersionUID = 2728437513842150372L;
private int pageSize;
private int start;
private List data;
private long totalCount;
public Page() {
this(0, 0L, 20, ((List) (new ArrayList())));
}
public Page(int start, long totalSize, int pageSize, List data) {
this.pageSize = 20;
if (pageSize <= 0 || start < 0 || totalSize < 0L) {
throw new BaseUnCheckedException("Illegal Arguments!");
} else {
this.pageSize = pageSize;
this.start = start;
totalCount = totalSize;
this.data = data;
return;
}
}
public long getTotalCount() {
return totalCount;
}
public long getTotalPageCount() {
return totalCount % (long) pageSize != 0L ? totalCount
/ (long) pageSize + 1L : totalCount / (long) pageSize;
}
public int getPageSize() {
return pageSize;
}
public void setResult(List data) {
this.data = data;
}
public List getResult() {
return data;
}
public int getCurrentPageNo() {
return start / pageSize + 1;
}
public boolean hasNextPage() {
return (long) getCurrentPageNo() < getTotalPageCount();
}
public boolean hasPreviousPage() {
return getCurrentPageNo() > 1;
}
public boolean isEmpty() {
return data == null || data.isEmpty();
}
public int getStartIndex() {
return (getCurrentPageNo() - 1) * pageSize;
}
public int getEndIndex() {
int endIndex = getCurrentPageNo() * pageSize - 1;
return (long) endIndex < totalCount ? endIndex : (int) totalCount - 1;
}
protected static int getStartOfPage(int pageNo) {
return getStartOfPage(pageNo, 20);
}
public static int getStartOfPage(int pageNo, int pageSize) {
return (pageNo - 1) * pageSize;
}
}
此类的对象解析成json格式的数据为:
{"currentPageNo":1,"data":[],"empty":true,"endIndex":-1,"pageSize":20,"result":[],"startIndex":0,"totalCount":0,"totalPageCount":0}
出现"result"是理所当然的, 我不明白这里为什么会用这个"data",因为它是私有属性,而且也没有getData()方法来访问它,怎么会得出data呢?
另外,我写了个测试类(作为对比,请你参考一下):
package com.ufida.leaf.ajaxservice.internal;
import java.util.ArrayList;
import java.util.List;
public class TestObject {
private String name="liuwenhui";
private int no=10086;
private String sex="M";
private List children=new ArrayList();
public String getRenameSex(){
return sex;
}
public List getSons(){
return children;
}
}
输出结果是:
{"renameSex":"M","sons":[]}
[b]问题补充:[/b]
现在的问题是:
上面的Page的对象解析成json格式的数据为:
{"currentPageNo":1,"data":[],"empty":true,"endIndex":-1,"pageSize":20,"result":[],"startIndex":0,"totalCount":0,"totalPageCount":0}
我不明白这里为什么会有这个"data",因为它是私有属性,而且也没有getData()方法来访问它,怎么会得出data呢? 按理说在这里不应该出现"data"属性啊
[b]问题补充:[/b]
回一楼的lovewhzlq ,那怎么取私有属性,连私有属性都可以随便访问,那不是java的安全机制有问题吗?
另外,既然您说可能是取到了私有属性,那我在上面提到的TestObject 里面的私有属性还是没有被解析到json中去呢~
[b]问题补充:[/b]
回四楼的pan_java ,请您看看我写的那个测试类TestObject您就明白我为什么这么问了~~
[b]问题补充:[/b]
回lovewhzlq 我用的json包是 json-lib-2.2.1-jdk15.jar,
我看了您的访问私有变量的方法,那是因为您重写了toString方法,相当于你自己还是提供了访问提口给它,因为System.out.println(p)实际会执行p.toString(); 然后再打印出来的,这跟反射机制应该没有什么关系吧~~呵呵~~
我用了反射机制取不到那个data.
[b]问题补充:[/b]
我找到问题的原因了,下面是我从page.class中反编译过来的类,请看:
[code="java"]
package com.ufida.console.common.service;
import com.sitechasia.webx.core.exception.BaseUnCheckedException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class Page
implements Serializable
{
private static final long serialVersionUID = -2073474076L;
private int pageSize;
private int start;
private List data;
private long totalCount;
public Page()
{
this(0, 0L, 20, new ArrayList());
}
public Page(int start, long totalSize, int pageSize, List data) {
this.pageSize = 20;
if ((pageSize <= 0) || (start < 0) || (totalSize < 0L))
throw new BaseUnCheckedException("Illegal Arguments!");
this.pageSize = pageSize;
this.start = start;
this.totalCount = totalSize;
this.data = data;
}
public long getTotalCount()
{
return this.totalCount;
}
public long getTotalPageCount()
{
return ((this.totalCount % this.pageSize != 0L) ? this.totalCount /
this.pageSize + 1L : this.totalCount / this.pageSize);
}
public int getPageSize() {
return this.pageSize;
}
public void setResult(List data) {
this.data = data;
}
public List getResult() {
return this.data;
}
public int getCurrentPageNo() {
return (this.start / this.pageSize + 1);
}
public boolean hasNextPage() {
return (getCurrentPageNo() < getTotalPageCount());
}
public boolean hasPreviousPage() {
return (getCurrentPageNo() > 1);
}
public boolean isEmpty() {
return ((this.data == null) || (this.data.isEmpty()));
}
public int getStartIndex() {
return ((getCurrentPageNo() - 1) * this.pageSize);
}
public int getEndIndex() {
int endIndex = getCurrentPageNo() * this.pageSize - 1;
return ((endIndex < this.totalCount) ? endIndex : (int)this.totalCount - 1);
}
protected static int getStartOfPage(int pageNo) {
return getStartOfPage(pageNo, 20);
}
public static int getStartOfPage(int pageNo, int pageSize) {
return ((pageNo - 1) * pageSize); }
public List getData() {
return this.data;
}
}
[/code]
这里面最后竟然有这个方法
[code="java"]
public List getData() {
return this.data;
}
[/code]
不知道什么时候编译器留下的,好气人啊,我都重新编译了一遍都没有更新到~~
感谢大家关注,特别感谢lovewhzlq,的确在您给的代码里我学到运用反射机制改变私有变量的方法~~虽然分不多,还是意思一下哈