【发布时间】:2014-07-13 00:09:03
【问题描述】:
请浏览整个问题以获得完整的想法。
首先让Box类给出如下:-
public class Box <T>{
private T t;
public void set(T t){
this.t = t;
System.out.println("value:\n");
System.out.printf("%s", t.toString());
}
public T get() {
return t;
}
static int retInt(){
return 5;
}
public <U extends Number> void inspect(U u){
System.out.println("T: " + t.getClass().getName());
System.out.println("U: " + u.getClass().getName());
}
}
通用类Util 如下所示:-
public class Util<T>{
private T t;
//Generic method
public <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
return p1.getKey().equals(p2.getKey()) &&
p1.getValue().equals(p2.getValue());
}
/* Static generic or non-generic methods can be declared in generic class
but they can not make use of generic parameter type(as generics static
methods using class type variable must know the type argument
(i.e value of type parameter); and knowledge of type argument is
possible only when object of same generic class are instantiated
(meaning assigning value of generic type parameter <T> or better to
say declared object have it's type argument; for example
as in List<T> replace T with Integer,String, Float etc);
but static method may be called without having
instance of class; so such declaration for static generic method are
not allowed) here it is <T>; like for example as shown below
public static int checkFun(T t){
return 5;
} // this generate compiler error complaining "can not make static
// reference to non-static type T".
*/
public static <K> boolean cmp(Box<K> b1, Box<K> b2){
// implement comparator to compare but here
return true;
}
// Inner class Pair
public class Pair <K, V> {
private K key;
private V value;
// Generic constructor
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public void setKey(K key) {
int i = 6;
if(i >4 || i<9);
this.key = key;
}
public void setValue(V value) {
this.value = value;
}
public K getKey(){
return key;
}
public V getValue(){
return value;
}
}
public void main1() {
//The complete syntax for invoking this method would be:
// <Integer, String> new Util<T>().
Pair<Integer, String> p1 = new Pair<Integer,String>(1, "apple");
Pair<Integer, String> p2 = new Pair<Integer, String>(2, "pear");
boolean same = compare(p1, p2);
//boolean same = true;
if(same)System.out.println("it is true: they are the same");
else System.out.println("nah! they are not the same...");
//boolean sm = compare();
}
public static void main (String[] args) /*throws FileNotFoundException */{
//new Util<Integer>(). main1();
Util<Integer> util = new Util<>();
util.main1();
}
}
上面的代码编译执行都很好,我的不适就在这里:
如果我们给方法添加static修饰符
public <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) -------(1)
// called in method main1()
并实现它
public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) -------(2)
然后编译器会抱怨 不能对非静态类型 Pair 进行静态引用,而类似的方法
public static <K> boolean cmp(Box<K> b1, Box<K> b2) -------(3)
这也是静态的,但不会抱怨。即使我们在but 和big but 两种方法中都没有使用类型参数<T>,但在我们从eq-1 谈论的方法中,它使用的参数来自内部类Pair(因此可以解释我的模棱两可的原因参考此功能)。
但还是;从逻辑上讲,我觉得在eq-1 中的方法中添加修饰符static 不应该产生编译时错误,因为无论在哪里调用eq-2 中的方法,该方法都将负责以正确的参数调用eq-2 中的方法并且应该允许像静态方法一样调用它。
问题:- 方法中不使用 static 修饰符的解释是什么:
public <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2)
感谢您的帮助。
【问题讨论】:
标签: java generics static inner-classes