我遇到getInstance
方法的问题,我尝试的任何方式:
Cannot make a static reference to the non-static type T.
我怎么能让这个工作?任何代码审查也将是一个很好的帮助。 我检查了几个相关的帖子,但这似乎是一个特殊的情况。
我的代码:
public class Data<Template>
{
private Template[] data;
private int id;
public Data(Template[] data)
{
this.data = data;
id = Arrays.hashCode(data);
log();
}
public Template[] getData()
{
return data;
}
public Integer getId()
{
return id;
}
private void log()
{
System.out.println(this.toString());
}
@Override
public String toString()
{
StringBuilder tString = new StringBuilder();
tString.append(id + ":");
for (int i = 0; i < data.length; i++)
{
tString.append(data[i] + " ");
}
return tString.toString();
}
}
public class Store<T>
{
private Queue<Data<T>> queue;
private ConcurrentHashMap<Integer, Data<T>> inProgress;
private List<T> results;
private static Store instance = null;
private Store()
{
queue = new ConcurrentLinkedQueue<Data<T>>();
inProgress = new ConcurrentHashMap<Integer, Data<T>>(16, 0.9f, 1);
results = new ArrayList<>();
}
public void addToStore(Data<T> elment)
{
queue.offer(elment);
}
public Data<T> getNextTask()
{
Data<T> element = queue.poll();
inProgress.put(element.getId(), element);
return element;
}
public void taskFinishedSuccessfully(Integer id, T result)
{
inProgress.remove(id);
results.add(result);
}
public List<T> getResults()
{
return results;
}
public static Store getInstance(Class<T> type)
{
if (instance == null)
{
if (type instanceof Integer)
{
instance = new Store<Integer>();
}
if (type instanceof Float)
{
instance = new Store<Float>();
}
if (type instanceof Double)
{
instance = new Store<Double>();
}
}
return instance;
}
}
答案 0 :(得分:5)
目前尚不清楚为什么你不只是写作
public static <T> Store<T> getInstance() {
return new Store<T>();
}
答案 1 :(得分:1)
所以这就是我最终做的事情:
public class Store<T> {
private Queue<Data<T>> queue;
private ConcurrentHashMap<Integer, Data<T>> inProgress;
private List<T> results;
private static Store<Double> doubleInstance = null;
private static Store<Integer> integerInstance = null;
private static Store<Float> floatInstance = null;
private Store() {
queue = new ConcurrentLinkedQueue<Data<T>>();
inProgress = new ConcurrentHashMap<Integer, Data<T>>(16, 0.9f, 1);
results = new ArrayList<>();
}
public void addToStore(Data<T> elment) {
queue.offer(elment);
}
public Data<T> getNextTask() {
Data<T> element = queue.poll();
inProgress.put(element.getId(), element);
return element;
}
public void taskFinishedSuccessfully(Integer id, T result) {
inProgress.remove(id);
results.add(result);
}
public List<T> getResults() {
return results;
}
private static <T> Store<T> getInstance() {
return new Store<T>();
}
public static Store<Double> getDoubleInstance() {
if (doubleInstance == null) {
doubleInstance = Store.<Double> getInstance();
}
return doubleInstance;
}
public static Store<Integer> getIntegerInstance() {
if (integerInstance == null) {
integerInstance = Store.<Integer> getInstance();
}
return integerInstance;
}
public static Store<Float> getFloatInstance() {
if (floatInstance == null) {
floatInstance = Store.<Float> getInstance();
}
return floatInstance;
}
}