Main Class

package Comparator.Bean;

import java.math.BigDecimal;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
* ? extends E : 接收E或者是E的子类对象。上限
* ? super E :接收E或者E的父类对象。下限
* @author asus
*
*/
public class ComparatorMain {
public static void main(String[] args) {
System.out.println("----------extends---------------");
List<People> peoples = new ArrayList<People>();
peoples.add(new People("Tom",22));
peoples.add(new People("Kitty",18));
peoples.add(new People("Marry",20));
printCollectionExtends(peoples);
List<Worker> workers = new ArrayList<Worker>();
workers.add(new Worker("Programmer", new BigDecimal(20000)));
workers.add(new Worker("teacher", new BigDecimal(6000)));
printCollectionExtends(workers);
List<String> strings = new ArrayList<String>();
strings.add("abc");
strings.add("efg");
strings.add("hij");;
//printCollection(strings);
System.out.println("----------super---------------");
printCollectionSuper(peoples);
List<Student> students = new ArrayList<Student>();
students.add(new Student(87, 96));
students.add(new Student(100, 100));
//printCollectionSuper(students);
List<Human> humans = new ArrayList<Human>();
humans.add(new Human("China", "1991-6-4"));
humans.add(new Human("US", "2000-6-4"));
printCollectionSuper(humans);
//printCollectionExtends(humans);
} public static void printCollectionExtends(Collection<? extends People> collection){
Iterator<?> iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
} public static void printCollectionSuper(Collection<? super People> collection){
Iterator<?> iterator = collection.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next().toString());
}
}
}

People class

package Comparator.Bean;

public class People extends Human{
private String name;
private int age;
private Student student;
private Worker worker; public People() {
super();
}
public People(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Worker getWorker() {
return worker;
}
public void setWorker(Worker worker) {
this.worker = worker;
} public String toString(){
return "name:"+name+";age:"+age;
} }

Human class

package Comparator.Bean;

public class Human {
private String country;
private String birthday; public Human(){} public Human(String country, String birthday) {
this.country = country;
this.birthday = birthday;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
} public String toString(){
return "country:"+country+"birthday:"+birthday;
}
}

Worker class

package Comparator.Bean;

import java.math.BigDecimal;

public class Worker extends People{
private String occupation;
private BigDecimal salary; public Worker(String occupation, BigDecimal salary) {
this.occupation = occupation;
this.salary = salary;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public String toString(){
return "Occupation:"+occupation+";salary:"+salary;
}
}

Student class

package Comparator.Bean;

public class Student extends People{
private int ChineseScores;
private int MathScores; public Student(int chineseScores, int mathScores) {
ChineseScores = chineseScores;
MathScores = mathScores;
}
public int getChineseScores() {
return ChineseScores;
}
public void setChineseScores(int chineseScores) {
ChineseScores = chineseScores;
}
public int getMathScores() {
return MathScores;
}
public void setMathScores(int mathScores) {
MathScores = mathScores;
} }

泛型的上限和下限的Demo的更多相关文章

  1. java 泛型的上限与下限

    设置泛型对象的上限使用extends,表示参数类型只能是该类型或该类型的子类: 声明对象:类名<? extends 类> 对象名 定义类:类名<泛型标签 extends 类>{ ...

  2. java中泛型上限,下限应用

    v 一.程序中无形之中用到的泛型 import java.util.*; class Person implements Comparable<Person>{ String name; ...

  3. Java笔记2 : 泛型的体现,及其上限、下限、通配符

    Java中的泛型是在jdk5.0引入的,语法不难,但是需要注意的细节有很多,这里写一下备忘. 首先是最简单的泛型类,泛型方法,泛型接口: //泛型接口的定义 interface MyInter< ...

  4. 牛客网Java刷题知识点之泛型概念的提出、什么是泛型、泛型在集合中的应用、泛型类、泛型方法、泛型接口、泛型限定上限、泛型限定下限、 什么时候使用上限?泛型限定通配符的体现

    不多说,直接上干货! 先来看个泛型概念提出的背景的例子. GenericDemo.java package zhouls.bigdata.DataFeatureSelection; import ja ...

  5. Java泛型-通配符的上限和下限问题

    Java的泛型中,通配符可以设置上限和下限. 上限:<? extends T> ?是T和T的子类 下限:<? super T> ?是T和T的父类 怎么看待这个上限和下限呢 首先 ...

  6. Java的泛型中,通配符可以设置上限和下限

    上限:<? extends T> ?是T和T的子类 下限:<? super T> ?是T和T的父类 怎么看待这个上限和下限呢 首先应该想 其实对于Java来说 <? ex ...

  7. WInform中实现设置ZedGraph中曲线的X轴与Y轴的上限与下限

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  8. Java 泛型通配符上限和通配符下限

    ①为什么要使用泛型通配符 请参考这篇随笔的下半部分 https://www.cnblogs.com/baxianhua/p/9194369.html   ②通配符上限和通配符下限的简单解释 <? ...

  9. js生成一个上限跟下限的随机数

    function sj() { //x上限,y下限 var x = 2000; var y = 1800; var rand = parseInt(Math.random() * (x - y + 1 ...

随机推荐

  1. mm/makefile

    ## Makefile for the linux memory manager.## Note! Dependencies are done automagically by 'make dep', ...

  2. testNG设置测试的执行顺序

    在java类中,设置Test的执行顺序可以使用priority,或者enabled等属性.但是在testng.xml中,需要设置它的 preserve-order="true" 另 ...

  3. [转]没有了SA密码,无法Windows集成身份登录,DBA怎么办?

    没有了SA密码,无法Windows集成身份登录,DBA怎么办?  原文:http://www.cnblogs.com/i6first/p/3512779.html 一同事反馈SQL无法正常登录了,以前 ...

  4. MSSQL 和 REDIS的数据类型对应关系

    when user_type_id in (34) then 'BLOB' --image            when user_type_id in (35) then 'CLOB' --tex ...

  5. 再次理解javascript中的事件

    一.事件流的概念 + 事件流描述的是从页面中接收事件的顺序. 二.事件捕获和事件冒泡 +    事件冒泡接收事件的顺序:

  6. 框架--NoHttp和OkHttp哪个好用,Volley和NoHttp哪个好用?

    NoHttp和OkHttp哪个好用,Volley和NoHttp哪个好用? NoHttp 源码及Demo托管在Github欢迎大家Star: https://github.com/Y0LANDA/NoH ...

  7. work flow

  8. Chap6: question38 - 42

    38. 数字 k 在有序数组中出现的次数 二分查找:找出第一个 k 和最后一个 k . #include <iostream> using namespace std; int getFi ...

  9. 22. Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  10. zhuang 定制iOS 7中的导航栏和状态栏

    近期,跟大多数开发者一样,我也正忙于对程序进行升级以适配iOS 7.最新的iOS 7外观上有大量的改动.从开发者的角度来看,导航栏和状态栏就发生了明显的变化.状态栏现在是半透明的了,这也就意味着导航栏 ...