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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李建强 中级黑马   /  2012-9-25 13:26  /  1198 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李建强 于 2012-9-25 13:41 编辑
  1. /*
  2. * 这个代码是来演示泛型限定的。

  3. */

  4. import java.util.*;
  5. class GenericDemo6
  6. {
  7. public static void main(String[] args)
  8. {
  9. ArrayList <Student> al1 = new ArrayList <Student>();
  10. al1.add(new Student("abc1"));
  11. al1.add(new Student("abc2"));
  12. al1.add(new Student("abc3"));
  13. printColl(al1);


  14. }
  15. public static void printColl(ArrayList<? extends Person>al)
  16. {
  17. Iterator <? extends Person> it = al.iterator();
  18. while (it.hasNext())
  19. {
  20. System.out.println(it.next().getName());
  21. }
  22. }

  23. }

  24. class Person
  25. {
  26. private String name;
  27. Person(String name)
  28. {
  29. this.name = name;
  30. }

  31. public String getName()
  32. {
  33. return name;
  34. }
  35. }

  36. class Student extends Person
  37. {

  38. }
复制代码
这段代码编译不能通过,提示:无法将类student中的构造器student应用到指定类型。

2 个回复

倒序浏览
Student 继承了Person
但是他默认的是调用Person的空构造函数即  Person(){};
所以建类的时候无法初始化
改为这样应该就没问题了你试试
class Student extends Person
{
Student(String name)
{supe(String name);}
}

评分

参与人数 1技术分 +1 收起 理由
王海宇 + 1

查看全部评分

回复 使用道具 举报
郭阳 发表于 2012-9-25 13:38
Student 继承了Person
但是他默认的是调用Person的空构造函数即  Person(){};
所以建类的时候无法初始化

3Q,疏忽了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马