import java.util.*;
public class TestSet
{
public static void main(String[] args)
{
Set s1 = new HashSet();
Set s2 = new HashSet();
s1.add("a");s1.add("b");s1.add("c");
s2.add("b");s2.add("d");s2.add("c");
Set s3 = new HashSet(s1);
s3.retainAll(s2);
Set s4 = new HashSet(s1);
s4.addAll(s2);
System.out.println(s3);
System.out.println(s4);
}
}
class HashSet
{
HashSet(){}
}
public class TestSet
{
public static void main(String[] args)
{
Set s1 = new HashSet();
Set s2 = new HashSet();
s1.add("a");s1.add("b");s1.add("c");
s2.add("b");s2.add("d");s2.add("c");
Set s3 = new HashSet(s1);
s3.retainAll(s2);
Set s4 = new HashSet(s1);
s4.addAll(s2);
System.out.println(s3);
System.out.println(s4);
}
}
new java.util.HashSet();
public class TestSet
{
public static void main(String[] args)
{
Set s1 = new java.util.HashSet();
Set s2 = new java.util.HashSet();
s1.add("a");s1.add("b");s1.add("c");
s2.add("b");s2.add("d");s2.add("c");
Set s3 = new java.util.HashSet(s1);
s3.retainAll(s2);
Set s4 = new java.util.HashSet(s1);
s4.addAll(s2);
System.out.println(s3);
System.out.println(s4);
}
}
class HashSet
{
HashSet(){}
}
输出为:
C:\>java TestSet
[c, b]
[d, a, c, b]
{
HashSet(){}
}
就能编译了
这个程序中虽然定义了自己的HashSet但是类加载器并没有加载这个类。所以不报错。
只要你使用自己的HashSet类必然会错。因为自己定义的HashSet不能向上塑型成Set.
不是分不清吧,按照楼主的写法new HashSet();一句中new 的就是自己定义的HashSet,这应该是个转型错误,自己的HashSet没办法转型成Set造成的。