A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© troy健 中级黑马   /  2014-7-25 08:59  /  1836 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public class HashSet {

  2.         public static void main(String[] args) {
  3.                 // TODO Auto-generated method stub
  4.                  HashSet hs = new HashSet();
  5.                 Person1 p1 = new Person1("zhangsa", 11);
  6.                 Person1 p2 = new Person1("lisi", 13);
  7.                 hs.add(p1);
  8.                 hs.add(p2);
  9.                 System.out.println(hs);
  10.         }
  11. }
复制代码

如果定义hs为HashSet,调用add方法就会出现下面问题
  1. The method add(Person1) is undefined for the type HashSet
复制代码


而把hs定义为Set就不会出现这样的问题
  1. Set hs = (Set)new HashSet();
复制代码


这是什么原因呢?

4 个回复

倒序浏览
你的类名有亮点   要么换类名 要么在新建HashSet时前面加java.util.

点评

java访问是就近原则  发表于 2014-7-25 09:56
回复 使用道具 举报
沙发正确~
回复 使用道具 举报
路过进来看看
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马