【泛型】泛型限定 ?(上限和下限案例)

/*泛型限定  ? extend E :可以接收E类型或E类型的子类类型,就确定了上限

? super E :可以接收E类型或E类型的父类类型,确定了下限

*/

泛型限定

package fx;
import java.util.*;
class Personfxxd
{
private String name;
private int age;
public Personfxxd(){}
public Personfxxd(String name,int age)
{
this.name=name;
this.age=age;
}
public String toString()
{
return name+","+age;
}
}
class Studentfxxd extends Personfxxd
{
public Studentfxxd(){}
public Studentfxxd(String name,int age)
{
super(name,age);
}
}
class Workerfxxd extends Personfxxd
{
public Workerfxxd(){}
public Workerfxxd(String name,int age)
{
super(name,age);
}
}


public class TestFXXD {
public static void main(String[] args) {
ArrayList<Studentfxxd> list1=new ArrayList<Studentfxxd>();
list1.add(new Studentfxxd("lisi",20));
list1.add(new Studentfxxd("yoti",27));
list1.add(new Studentfxxd("mm",21));

dieDaiQi(list1);
ArrayList<Workerfxxd> list2=new ArrayList<Workerfxxd>();
list2.add(new Workerfxxd("lisi",25));
list2.add(new Workerfxxd("yoti",28));
list2.add(new Workerfxxd("mm",24));
dieDaiQi(list2);
}
//使用迭代器
//? extends 可以接收E类型或E类型的子类类型,就确定了上限,也就是可以是Personfxxd 也可以是Studentfxxd类
public static void dieDaiQi(Collection<? extends Personfxxd> col)//?是通配符,什么类型都可以
{
Iterator<? extends Personfxxd> ite= col.iterator();
while(ite.hasNext())
{
Personfxxd person=ite.next();
System.out.println(person);
}
}

}



泛型的上限的使用

package fx;
import java.util.*;;
public class TestFxSx {


/**
* @param args
*/
public static void main(String[] args) {
//泛型的上限的使用 
TreeSet<Studentfxxd> ts1=new TreeSet<Studentfxxd>();
ts1.add(new Studentfxxd("lisi",21));
ts1.add(new Studentfxxd("yoti",23));
ts1.add(new Studentfxxd("wangli",20));

TreeSet<Workerfxxd> ts2=new TreeSet<Workerfxxd>();
ts2.add(new Workerfxxd("lisi",22));
ts2.add(new Workerfxxd("yoti",44));
ts2.add(new Workerfxxd("wangli",34));

//TreeSet(Collection<? extends E> c)
//相当于,继承了Personfxxd类,
TreeSet<Personfxxd> ts=new TreeSet<Personfxxd>(ts1);
TreeSet<Personfxxd> ts3=new TreeSet<Personfxxd>(ts2);
System.err.println(ts1);

}
}



//泛型下限的使用
package fx;  
import java.util.*;
class ComByNameS implements Comparator<Studentfxxd>
{

public int compare(Studentfxxd stu1,Studentfxxd stu2)
{
//按照姓名排序
int num=stu1.getName().compareTo(stu2.getName());
return num==0?stu1.getAge()-stu2.getAge():num;//2个年龄相减看是否等于0.如果等于0.就取名字,不等,就取第2个名字
}
}
class ComByNameW implements Comparator<Workerfxxd>
{
public int compare(Workerfxxd w1,Workerfxxd w2)
{
int num=w1.getName().compareTo(w2.getName());
return num==0?w1.getAge()-w2.getAge():num;
}
}


//不管是Workerfxxd类还是Studentfxxd都可以,其实只需要填写此父类即可,这样就可以继承了他们对应的父类。
class ComByNameHZ implements Comparator<Personfxxd>
{
public int compare(Personfxxd sw1,Personfxxd sw2)
{
int num=sw1.getName().compareTo(sw2.getName());
return num==0?sw1.getAge()-sw2.getAge():num;
}
}
public class TestFxXs {
public static void main(String[] args) {
//TreeSet(Comprator<? super E>comparator)
// 以上有多少个子类就要弄几个,很麻烦,因此会有到下限
// ComByNameS coms=new ComByNameS();
// TreeSet<Studentfxxd> ts1=new TreeSet<Studentfxxd>(coms); //集合1
// ts1.add(new Studentfxxd("lisi",20));
// ts1.add(new Studentfxxd("yoti",28));
// ts1.add(new Studentfxxd("mm",22));
// System.out.println(ts1);//输出效果:[lisi,20, mm,22, yoti,28]
// ComByNameW comw=new ComByNameW();
// TreeSet<Workerfxxd> ts2=new TreeSet<Workerfxxd>(comw);//集合2
// ts2.add(new Workerfxxd("lisi",22));
// ts2.add(new Workerfxxd("zhaosi",24));
// ts2.add(new Workerfxxd("mm",29));
// System.out.println(ts2);//输出效果:[lisi,22, mm,29, zhaosi,24]

//好处是,如果只有很多子类时,只需要一个类即可
ComByNameHZ comsw=new ComByNameHZ();
TreeSet<Studentfxxd> ts1=new TreeSet<Studentfxxd>(comsw); //集合1
ts1.add(new Studentfxxd("lisi",20));
ts1.add(new Studentfxxd("yoti",28));
ts1.add(new Studentfxxd("mm",22));
System.out.println(ts1);//输出效果:[lisi,20, mm,22, yoti,28]
ComByNameW comw=new ComByNameW();
TreeSet<Workerfxxd> ts2=new TreeSet<Workerfxxd>(comsw);//集合2
ts2.add(new Workerfxxd("lisi",22));
ts2.add(new Workerfxxd("zhaosi",24));
ts2.add(new Workerfxxd("mm",29));
System.out.println(ts2);//输出效果:[lisi,22, mm,29, zhaosi,24]


}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值