黑马程序员技术交流社区
标题:
泛型下限问题
[打印本页]
作者:
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
package cn.lu.test;
import java.util.ArrayList;
import java.util.Iterator;
class People{
private String name;
public String getName(){
return name;
}
public void print(){
System.out.println("this is People Class");
}
public void peoplePrint(){
System.out.println("this is People Class");
}
}
class Person1 extends People{
private String name;
public Person1(String name){
this.name=name;
}
public String getName(){
return name;
}
public void person1Print(){
System.out.println("this is Student1 Class");
}
}
class Student1 extends Person1{
public Student1(String name){
super(name);
}
public void print(){
System.out.println("this is Student1 Class");
}
public void Studentprint(){
System.out.println("this is Student1 Class");
}
}
class Student2 extends Person1{
public Student2(String name){
super(name);
}
public void print(){
System.out.println("this is Student2 Class");
}
}
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(al);
}
//利用泛型限定,上限
public static void printArr(ArrayList<? extends Person1> al){
Iterator<? extends Person1> it=al.iterator();
while(it.hasNext()){
<font color="Blue">it.next().Studentprint();</font> <font color="Blue"><b>/*在Student1中加入他自己独有的方法,Studentprint() 此处如果不强制类型转换,会出错, 因为Person1中没有该方法。因为泛型定义的是Person1的所有子类,而所有子类都继承了或覆盖(重写)了Person1中的非私有方法,所以直接调用会调用子类覆盖了的方法即实现多态,或者继承过来的方法。*/</b> </font>
}
}
//下限
public static void print2(ArrayList<? super Student1> al){
Iterator<? super Student1> it=al.iterator();
while(it.hasNext()){
((People) it.next()).print();<b><font color="RoyalBlue">/*泛型定义的是Student1的父类,或者父类的父类等,而Student1的父类Person1与Person1的父类People中的方法是不一致的,也就是不能同过子类调用父类的方法,编译器也不知道你要调用那个类的方法,所以需要强制类型转换,告诉编译器,你要调用哪个类的方法。*/</font></b>
}
}
}
复制代码
我个人的理解,如有不对,谢谢指正。
作者:
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