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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 黄长利 中级黑马   /  2012-4-1 20:03  /  2685 人查看  /  9 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;
  2. class GenericLimit
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                
  7.                 ArrayList<Person> al = new ArrayList<Person>();
  8.                 al.add(new Person("asdh12"));
  9.                 al.add(new Person("sdsds12"));
  10.                 al.add(new Person("bsdgf12"));
  11.                 al.add(new Person("dsfda12"));
  12.                 printColl(al);
  13.                
  14.                 ArrayList<Student> al1 = new ArrayList<Student>();
  15.                 al1.add(new Student("wushen"));
  16.                 al1.add(new Student("juedui"));
  17.                 al1.add(new Student("chixin"));
  18.                 printColl(al1);
  19.                
  20.                
  21.         }
  22.         public static void printColl(ArrayList<? super Student> al) //此处限定泛型的下限 Student
  23.         {
  24.                 Iterator<? super Student> it = al.iterator();
  25.                 while(it.hasNext())
  26.                 {
  27.                         System.out.println(it.next().getName());
  28.                 }
  29.         }
  30. }

  31. class Person
  32. {
  33.         private String name;
  34.         Person(String name)
  35.         {
  36.                 this.name = name;
  37.         }
  38.         public String getName()
  39.         {
  40.                 return name;
  41.         }
  42. }

  43. class Student extends Person
  44. {
  45.         Student(String name)
  46.         {
  47.                 super(name);
  48.         }
  49. }
复制代码
以上代码在 printColl函数的参数位置,限定泛型的上限 使用 “? extends Person” 编译和运行没有问题,为什么使用下限方式就不能通过呢?

9 个回复

倒序浏览
使用<? super Student>,即表示可以接受Student及其父类,
要是你传进去的是Object类,当执行System.out.println(it.next().getName());时,
就会出问题:Object根本没有getName()方法!
所以使用下限方式不行.

回复 使用道具 举报
使用<? super Student>,即表示可以接受Student及其父类,
要是你传进去的是Object类,当执行System.out.println(it.next().getName());时,
就会出问题:Object根本没有getName()方法!
所以使用下限方式不行.

回复 使用道具 举报
Person 类有num属性是private的  子类Student无法继承的 所以子类student也就无法继承public String getName()这个方法,因为student没有num这个属性。。。。。然后你就通了吧。。。。

回复 使用道具 举报
刘基军 发表于 2012-4-1 23:13
使用

额  有理  我的那个错了。。。。
回复 使用道具 举报
刘基军 发表于 2012-4-1 23:13
使用

就是说只要有传进去 Object 类的可能,就不能使用 getName() 方法了是吧
回复 使用道具 举报
object也是Student的父类,但是Object没有getName()方法,所以不能编译通过。
直接写<Person>就行,传入Student 是没有问题的。前天我刚问过这个问题。
回复 使用道具 举报
package Test.com;

import java.util.*;

public class GenericLimit
{

        public static void main(String[] args)
        {

               
                ArrayList<Person> al = new ArrayList<Person>();

                al.add(new Person("asdh12"));

                al.add(new Person("sdsds12"));

                al.add(new Person("bsdgf12"));

                al.add(new Person("dsfda12"));

                printColl(al);

               
                ArrayList<Student> al1 = new ArrayList<Student>();

                al1.add(new Student("wushen"));

                al1.add(new Student("juedui"));

                al1.add(new Student("chixin"));

                printColl(al1);

               
               
        }

        public static void printColl(ArrayList<? super Student> al) //此处限定泛型的下限 Student

        {

                Iterator<? super Student> it = al.iterator();

                while(it.hasNext())

                {
                          Person s=(Person)it.next();
                        System.out.println(s.getName());

                }

        }

}


class Person

{

        private String name;

        Person(String name)

        {

                this.name = name;

        }

        public String getName()

        {

                return name;

        }

}


class Student extends Person

{

        Student(String name)

        {

                super(name);

        }

}
这样就可以了
回复 使用道具 举报
<? super Student>意思是说可以接收Student类型或Student的父类型,那么如果不能保证Student及其父类中都有getName()方法,就不能使用该方法。
那么<? super Student>可以改成<? extends Person>,这个意思是可以接收Person及其子类,因为Person类中有getName()方法,所以其子类中也自然都有getName()方法。
回复 使用道具 举报
Person 类是private的  子类Student无法继承的 所以子类student也就无法继承getName()这个方法,因为student不能用getName()这个方法,如把Person 类权限为public,则可以通过。
另一种是<? super Student>可以改成<? extends Person>,也可以通过。
还有一种是在迭代里把it.next强制转换成person
while(it.hasNext())

                {
                          Person s=(Person)it.next();
                        System.out.println(s.getName());

                }

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马