日期:2014-05-20  浏览次数:20655 次

父类引用指向子类对象Set s = new HashSet();
为什么在eclipse里面写:
Set s = new HashSet();

Collection c = new ArrayList();
都会报错:
Multiple markers at this line
- Type mismatch: cannot convert from HashSet to Set
- HashSet is a raw type. References to generic type HashSet<E> should be 
 parameterized

可直接用java命令是可以编译通过的
是不是跟泛型有关啊?
请知道的兄弟帮忙讲解一下,谢谢!

------解决方案--------------------
import java.util.*;
public class Test {

public static void main(String[] args){
Set s = new HashSet();
s.add("1");
System.out.println(s);
}
}

我的eclpise 可以运行,没有问题
eclipse版本
Version: Helios Service Release 2
Build id: 20110218-0911
(c) Copyright Eclipse contributors and others 2000, 2011.  All rights reserved.
Visit http://eclipse.org/




------解决方案--------------------
楼主进入Eclipse 然后 点 window -> preferences -> java -> Errors/warnings  然后看右手边有个Generic Types   有个Usage of a raw type  将选项由error改为waring或者ignore。 
该问题即可解决!