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. .jre下的lib和jdk下的lib的区别

    jre是JDK的一个子集.提供一个运行环境.JDK的lib目录是给JDK用的,例如JDK下有一些工具,可能要用该目录中的文件.例如,编译器等.JRE的lib目录是为JVM,运行时候用的.包括所有的标准 ...

  2. Date get period

    /** * get period for last year * @param time * @return */ public static DatePeriodDTO getLastYear(lo ...

  3. Extjs控制面板组件

    (1)aoolyTo:(id) renderTo:(id)呈现在哪个html里面,同上  id最好用"" contentEI:() 呈现哪个html元素里面,把eI内的内容呈现 ( ...

  4. [NOIP2009] 靶形数独(搜索+剪枝)

    题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他 们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教, Z 博士拿出了他最近发明的 ...

  5. 论文笔记之:Speed Up Tracking by Ignoring Features

    Speed Up Tracking by Ignoring Features CVPR 2014 Abstract:本文提出一种特征选择的算法,来实现用最"精简"的特征以进行目标跟 ...

  6. curl的POST与GET方法

      $url = '127.0.0.1/shang/bb.php';   $data = array('name'=>'赵猛','age'=>'23');   print_r(get($u ...

  7. Delphi下16进制位图数据转位图

    如果我们在Form中拖入一个Image控件,并设置好picture后,Alt+F12就可以看到Form的源代码中已经将图片转成了16进制字符串,如下: ? 1 2 3 4 5 6 7 8 9 10 1 ...

  8. 学习C++的第二天

    1.输入 十六进制  cin>>hex>>x>>dec>>y    dec 是十进制    oct 是八进制 输出 十六进制 cout<<h ...

  9. JVM参数(三)打印所有XX参数及值

    本篇文章基于Java 6(update 21oder 21之后)版本, HotSpot JVM 提供给了两个新的参数,在JVM启动后,在命令行中可以输出所有XX参数和值. -XX:+PrintFlag ...

  10. Spring Boot 静态资源处理

    spring Boot 默认的处理方式就已经足够了,默认情况下Spring Boot 使用WebMvcAutoConfiguration中配置的各种属性. 建议使用Spring Boot 默认处理方式 ...