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. canvas实现类似弹窗广告效果

    先看看下面的效果图,想想使用canvas是怎样实现的? 如下图: 这个就不详细描述了,看代码就会了. <!doctype html> <html lang="en" ...

  2. Qt 手动添加ui文件到工程(转)

    制作ui文件 先应该用Qt Designer绘制一个自己的界面,并存为myform.ui(这里的myform可以用自己喜欢的名字代替).在制作自己的界面文件时要注意以下几个要点: 1.要记住ui文件的 ...

  3. [原创]cocos2d-x研习录-第二阶 基本框架

    了解完Cocos2D-x的基本概念和概念类之后,是不是有一种蠢蠢欲动的冲动,想要探究Cocos2D-x是如何完成这一切的.接着我将通过对Cocos2D-x自代的HelloCpp项目进行分析,初步了解C ...

  4. 设计模式-UML类图的各符号含义(转)

    UML类图的各符号含义 类图基本符号可拆分为虚线,箭头,实线,空心右三角,实心右三角,空心菱形和实心菱形.由这些基本的图形进行组合构成了类图的基本符号.这里要注意这几个符号的顺序,代表了类与类之间关系 ...

  5. Docker SSH+NGHINX+MYSQL

    1.添加创建镜像的配置文件      vim Dockerfile      输入以下内容: FROM jdeathe/centos-sshMAINTAINER baxk"xxx@hongh ...

  6. SQL总结(七)查询实战

    SQL总结(七)查询实战 一.场景 给定一个场景,学生选课系统为例,大家很熟悉. 主要关系: 学生(学号.姓名.年龄.性别) 教师(教师ID,教师姓名) 课程(课程ID,课程名称,任教教师ID) 成绩 ...

  7. 微信公众平台开发教程(八)Session处理

    微信公众平台开发教程(八)Session处理 在微信窗口,输入的信息有限,我们需要将一些信息分多次请求. 比如:在进行用户绑定时,我们需要输入用户的相关信息,比如:用户名.密码,或者姓名.电话号码,服 ...

  8. ruby 查询mysql方法

    首先对需要使用的数据库进行封装,便于使用:数据库表封装源码: mysqlapi.rb #业务涉及的数据库的配置ActiveRecord::Base$db1={:adapter => " ...

  9. 让finder显示路径

    在控制台输入 defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES 重启finder即可.

  10. 'libxml/tree.h' file not found

    看看Header Search Paths 为  '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Dev ...