首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

父类引述指向子类对象Set s = new HashSet()

2013-08-01 
父类引用指向子类对象Set s new HashSet()为什么在eclipse里面写:Set s new HashSet()或Collection

父类引用指向子类对象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。 
该问题即可解决!

热点排行