我写了一个接口
public interface IA {
<T> void addNew(T t);
}
然后有个类实现接口IA
public class A impelements IA {
@Override
public <Person> void addNew(Person p) {
System.out.println(p);
}
}
这个时候类A会提示说方法addNew有警告说Type hiding from type什么的.
请问这个警告是什么意思?
我尝试用main方法调用, 比如
public class Test {
public static final main(String[] args) {
A a = new A();
a.addNew(new Person());
a.addNew("new Person()");
}
}
都可以调用, 但是只有a.addNew(new Person());这句有效果. 我认为这边应该报错的. 因为第二个方法实际没有实现方法.
最后问下我这样子想接口不指定具体的参数类型, 在实现类里面再来指定, 在其他类中调用时可以根据不同的类对象来
实现不同的参数类型是否可以实现?
------解决方案--------------------
- Java code
//泛型是这么用的class Person{ }interface IA<T> { void addNew(T t);}class A implements IA<Person>{ @Override public void addNew(Person p) { System.out.println("A:"+p); }}class B implements IA<String>{ @Override public void addNew(String p) { System.out.println("B:"+p); }}public class GenericTest { public static void main(String[] args) { A a = new A(); a.addNew(new Person()); B b = new B(); b.addNew("new Person()"); }}
------解决方案--------------------
<T> void addNew(T t);这个将自动转换传入的参数t的类型.
实现的时候,public <Person> void addNew(Person p),Person p就是将T替换为person类,<Person>不是像new对象时表示指定泛型类型,而是将void addNew(Person p)重新定义为一个泛型标示符为Person的泛型方法...这下懂了吧
------解决方案--------------------
可以参考下HashMap的源码
摘一段:
- Java code
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable{/** * Constructs an empty <tt>HashMap</tt> with the default initial capacity * (16) and the default load factor (0.75). */ public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); table = new Entry[DEFAULT_INITIAL_CAPACITY]; init(); }/** * Returns the value to which the specified key is mapped, * or {@code null} if this map contains no mapping for the key. * * <p>More formally, if this map contains a mapping from a key * {@code k} to a value {@code v} such that {@code (key==null ? k==null : * key.equals(k))}, then this method returns {@code v}; otherwise * it returns {@code null}. (There can be at most one such mapping.) * * <p>A return value of {@code null} does not <i>necessarily</i> * indicate that the map contains no mapping for the key; it's also * possible that the map explicitly maps the key to {@code null}. * The {@link #containsKey containsKey} operation may be used to * distinguish these two cases. * * @see #put(Object, Object) */public V get(Object key) { if (key == null) return getForNullKey(); int hash = hash(key.hashCode()); for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) return e.value; } return null; }/** * Associates the specified value with the specified key in this map. * If the map previously contained a mapping for the key, the old * value is replaced. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt>. * (A <tt>null</tt> return can also indicate that the map * previously associated <tt>null</tt> with <tt>key</tt>.) */ public V put(K key, V value) { if (key == null) return putForNullKey(value); int hash = hash(key.hashCode()); int i = indexFor(hash, table.length); for (Entry<K,V> e = table[i]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(hash, key, value, i); return null; }//////////////////////////////////////////*省略了很多*//////////////////////////////////////////}