初始化Java中的通用变量? [英] Initializing Generic Variables in Java?
本文介绍了初始化Java中的通用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用泛型时遇到了麻烦.我有一种方法
I'm having trouble working with generics. I have a method as
public void push (T element)
现在,我很难理解的是如何创建一个通用变量,以便可以将其传递给该方法.我知道泛型永远是一个数字,但是我不知道该怎么做.一定是
Now what I'm having trouble understanding is how to create a generic variable so that I can pass it into that method. I know that the generic will always be a number, but I'm not getting how I should do that. Would it have to be something like
T number = 5
然后我可以将其传递给push方法吗?我很困惑大家好吗?
And then would I be able to pass that into the push method? I'm quite confused. Thoughts guys?
推荐答案
您说T是数字
public class NumberStack<T extends Number> {
public void push(T element) {
// now you can add Number specific functionality here
}
}
这篇关于初始化Java中的通用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文