我正在Java中向泛型类添加泛型。我在修复代码时遇到麻烦,以避免在方法root@VM-0-8-ubuntu:/usr/bin# python -V
Python 3.7.2
root@VM-0-8-ubuntu:/usr/bin# pip3 -V
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ModuleNotFoundError: No module named 'pip'
root@VM-0-8-ubuntu:/usr/bin# sudo apt-get install python3-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-pip is already the newest version (8.1.1-2ubuntu0.4).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@VM-0-8-ubuntu:/usr/bin# pip3 -V
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ModuleNotFoundError: No module named 'pip'
root@VM-0-8-ubuntu:/usr/bin# sudo apt updaate
E: Invalid operation updaate
root@VM-0-8-ubuntu:/usr/bin# sudo apt update
Hit:1 http://mirrors.tencentyun.com/ubuntu xenial InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu xenial-security InRelease
Ign:3 http://mirrors.aliyun.com/ubuntu trusty InRelease
Hit:4 http://mirrors.tencentyun.com/ubuntu xenial-updates InRelease
Hit:5 http://mirrors.aliyun.com/ubuntu trusty-security InRelease
Hit:6 http://mirrors.aliyun.com/ubuntu trusty-updates InRelease
Hit:7 http://mirrors.aliyun.com/ubuntu trusty-proposed InRelease
Hit:8 http://mirrors.aliyun.com/ubuntu trusty-backports InRelease
Hit:9 http://mirrors.aliyun.com/ubuntu trusty Release
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
root@VM-0-8-ubuntu:/usr/bin# sudo apt upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@VM-0-8-ubuntu:/usr/bin# sudo apt-get install python3-pip
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-pip is already the newest version (8.1.1-2ubuntu0.4).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
root@VM-0-8-ubuntu:/usr/bin# pip3 -V
Traceback (most recent call last):
File "/usr/bin/pip3", line 9, in <module>
from pip import main
ModuleNotFoundError: No module named 'pip'
root@VM-0-8-ubuntu:/usr/bin#
警告:java:未经检查的演员
必需:java.util.ArrayList
发现:java.util.ArrayList
关于:
public ArrayList<T> getNextPage()
警告:java:未经检查的演员
必需:java.util.ArrayList
发现:java.util.ArrayList
关于:
return (ArrayList<T>) getPaginatedContracts(startRow, pageSize);
目前return (ArrayList<T>) getPaginatedJobs(startRow, pageSize);
可以是T
或Contract
。
还有没有其他方法可以返回Job
,而它正确地使用了泛型而没有任何警告?
或者因为我的构造函数使用ArrayList<T>
并进行了一些类型检查:
Class<T>
我可以在if (clazz.isAssignableFrom(Contract.class)) {...}
上使用@SuppressWarnings("unchecked")
吗?
getNextPage()
答案 0 :(得分:1)
您可以将该类抽象化,然后子类必须实现实际的数据库调用:
public abstract class PaginationWrapper<T> {
private int currentPage = 1;
private int pageSize = 100;
public ArrayList<T> getNextPage() {
currentPage++;
int startRow = currentPage * pageSize;
// here we delegate to the implementation
return getNextPage(startRow, pageSize);
}
protected abstract ArrayList<T> getNextPage(int startRow, int pageSize);
}
然后创建2个子类
public class JobPagination extends PaginationWrapper<Job> {
protected ArrayList<Job> getNextPage(int startRow, int pageSize) {
return /* list from database */
}
}
public class ContractPagination extends PaginationWrapper<Contract> {
protected ArrayList<Contract> getNextPage(int startRow, int pageSize) {
return /* list from database */
}
}
现在,一般逻辑被打包到PaginationWrapper
中,但是实际的数据库逻辑现在位于特定的子类中。