黑马程序员技术交流社区

标题: 泛型下限问题 [打印本页]

作者: yangruijing    时间: 2015-3-7 15:47
标题: 泛型下限问题
class Person1{
        private String name;
        public Person1(String name){
                this.name=name;
        }
        public String getName(){
                return name;
        }
}
class Student1 extends Person1{
        public Student1(String name){
                super(name);
        }
}
public class GenericDemo {

        public static void main(String[] args) {
                ArrayList<Person1> al=new ArrayList<Person1>();
                al.add(new Person1("zhagnsan1"));
                ArrayList<Person1> al1=new ArrayList<Person1>();
                al1.add(new Student1("liming1"));       
                printArr(al1);
        }
        //利用泛型限定,上限
        public static void printArr(ArrayList<? extends Person1> al){
                Iterator<? extends Person1> it=al.iterator();
                while(it.hasNext()){
                        System.out.println(it.next().getName());
                }
        }
        //下限
        public static void print2(ArrayList<? super Student1> al){
                Iterator<? super Student1> it=al.iterator();
                while(it.hasNext()){
                System.out.println(it.next().getName());
                }
        }       
}
在向下限定时Student1类继承了Person1类,而Person1类中有getName方法,那么it.next().getName()为什么不正确?


作者: 路文龙    时间: 2015-3-7 16:35
  1. package cn.lu.test;

  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4.    class People{
  5.            private String name;
  6.            public String getName(){
  7.            return name;
  8.            }
  9.          public void print(){
  10.                    System.out.println("this is People Class");
  11.            }
  12. public void peoplePrint(){
  13.    System.out.println("this is People Class");
  14. }
  15.    }
  16.         class Person1 extends People{
  17.         private String name;
  18.         public Person1(String name){
  19.                 this.name=name;
  20.         }
  21.         public String getName(){
  22.                 return name;
  23.         }
  24.         public void person1Print(){
  25.                    System.out.println("this is Student1 Class");
  26.            }
  27. }
  28. class Student1 extends Person1{
  29.         public Student1(String name){
  30.                 super(name);
  31.         }
  32.         public void print(){
  33.                  System.out.println("this is Student1 Class");
  34.          }
  35.         public void Studentprint(){
  36.                   System.out.println("this is Student1 Class");
  37.           }
  38. }
  39. class Student2 extends Person1{
  40.     public Student2(String name){
  41.             super(name);
  42.     }
  43.     public void print(){
  44.              System.out.println("this is Student2 Class");
  45.      }
  46. }
  47. public class GenericDemo {

  48.         public static void main(String[] args) {
  49.                 ArrayList<Person1> al=new ArrayList<Person1>();
  50.                 al.add(new Person1("zhagnsan1"));
  51.                 ArrayList<Person1> al1=new ArrayList<Person1>();
  52.                 al1.add(new Student1("liming1"));      
  53.                 printArr(al);
  54.         }
  55.         //利用泛型限定,上限
  56.         public static void printArr(ArrayList<? extends Person1> al){
  57.                 Iterator<? extends Person1> it=al.iterator();
  58.                 while(it.hasNext()){
  59.                        <font color="Blue">it.next().Studentprint();</font>  <font color="Blue"><b>/*在Student1中加入他自己独有的方法,Studentprint() 此处如果不强制类型转换,会出错, 因为Person1中没有该方法。因为泛型定义的是Person1的所有子类,而所有子类都继承了或覆盖(重写)了Person1中的非私有方法,所以直接调用会调用子类覆盖了的方法即实现多态,或者继承过来的方法。*/</b> </font>
  60.                 }
  61.         }
  62.         //下限
  63.         public static void print2(ArrayList<? super Student1> al){
  64.                 Iterator<? super Student1> it=al.iterator();
  65.                 while(it.hasNext()){
  66.                         ((People) it.next()).print();<b><font color="RoyalBlue">/*泛型定义的是Student1的父类,或者父类的父类等,而Student1的父类Person1与Person1的父类People中的方法是不一致的,也就是不能同过子类调用父类的方法,编译器也不知道你要调用那个类的方法,所以需要强制类型转换,告诉编译器,你要调用哪个类的方法。*/</font></b>
  67.                 }
  68.         }      
  69. }

复制代码
我个人的理解,如有不对,谢谢指正。

作者: yangruijing    时间: 2015-3-7 17:40
路文龙 发表于 2015-3-7 16:35
我个人的理解,如有不对,谢谢指正。

不明白,我的程序中Person1只有Student1一个子类,Student1也只有Person1一个父类呀
作者: yangruijing    时间: 2015-3-7 20:45
路文龙 发表于 2015-3-7 16:35
我个人的理解,如有不对,谢谢指正。

想了好久,终于明白了,你的理解是正确的,谢谢啦




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2