我在我的Web应用程序(使用Spring和Hibernate)中实现了分页,我需要的东西类似于以下内容.
public static int deleteSingle(long totalRows, long pageSize, int currentPage)
{
return totalRows==currentPage*pageSize-pageSize ? currentPage-- : currentPage;
}
Run Code Online (Sandbox Code Playgroud)
假设,我从以下某处调用此方法.
deleteSingle(24, 2, 13);
Run Code Online (Sandbox Code Playgroud)
使用这些参数,条件得到满足,并且currentPage应该返回变量(即13)减去1(即12)的值,但不会递减值currentPage.它返回此调用后的原始值13.
我必须像下面这样更改方法,以使其按预期工作.
public static int deleteSingle(long totalRows, long pageSize, int currentPage)
{
if(totalRows==currentPage*pageSize-pageSize)
{
currentPage=currentPage-1; //<-------
return currentPage; //<-------
}
else
{
return currentPage;
}
}
Run Code Online (Sandbox Code Playgroud)
那么为什么不用减量运算符将值减1 currentPage--呢?为什么需要 - currentPage=currentPage-1;在这种情况下?
在你的return语句中,它使用currentPage--哪个导致返回后的减量.你想--currentPage在回归之前做减量.就个人而言,有了这样一个复杂的陈述,你可能想要为了可读性而打破它,但这是一个偏好问题.
(从技术上讲,它在读完后会减少.没有什么特别的,它是一个返回语句,当它递减时会发生变化.)
如果由我决定,我的口味是这样做:
public static int deleteSingle(long totalRows, long pageSize, int currentPage)
{
if(totalRows==currentPage*pageSize-pageSize)
{
currentPage--;
}
return currentPage;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1902 次 |
| 最近记录: |