泛型的上限和下限的Demo

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;
    }

}
时间: 2024-12-11 15:51:49

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

泛型-通配符,受限泛型(上限和下限)

掌握通配符“?”的使用. 掌握受限泛型的设置. 掌握泛型与子类继承的限制. 具体内容 1.1匹配任意类型的通配符 在开发中对象的引用传递(向上向下传递)是最常见的,但是,在泛型的操作中,在进行引用传递的时候泛型类型必须匹配才可以传递,否则不能传递. 例如,如下没有进行泛型类型匹配,一个是String,一个是Object类型. package Thread1; class Info<T>{ private T var ; // 定义泛型变量 public void setVar(T var){

java 泛型的上限与下限

设置泛型对象的上限使用extends,表示参数类型只能是该类型或该类型的子类: 声明对象:类名<? extends 类> 对象名 定义类:类名<泛型标签 extends 类>{} 设置泛型对象的下限使用super,表示参数类型只能是该类型或该类型的父类: 声明对象:类名<? super 类> 对象名称 定义类:类名<泛型标签 extends类>{} public static void  show(List<? extends Number> l

java中泛型上限,下限应用

import java.util.*; class Person implements Comparable<Person>{ String name; int age; Person(){ name = ""; age = 0; } Person(String name, int age){ this.name = name; this.age = age; } public String toString(){ return name + "...."

期权价格的上限和下限

期权按照买方权利性质分为:看涨期权和看跌期权 1.首先,看涨期权的上限和下限 看涨期权价格上限为其标的资产价格. 看涨期权是给予买方一个在未来买入标的资产的权利,如果该权利的价格高于标的资产的价格,那么投资者不如直接购买标的资产. 换句话说,期权类似于保险,如果一栋房子的火灾险比房子还贵,那么我们还不如直接卖掉这样的保险,用获得的保费再买套房子.如果发生火灾,则直接将这套房子赔出去就好,并且由于保费比房子还贵,我们在买完房子之后,还能剩下一笔钱.无论是否发生火灾,我们都能稳赚这笔钱.因此,看涨期

Java 泛型通配符上限和通配符下限(正在整理)

https://www.jianshu.com/p/66bd729bc129 <? extends T>上限通配,表示?是T的一个未知子类. <? super T>下限通配,表示?是T的一个未知父类. 下面来分别讲一下 1. <? extends T> 上限通配 这里?表示一个未知的类,而T是一个具体的类,在实际使用的时候T需要替换成一个具体的类,表示实例化的时候泛型参数要是T的子类. 举个例子 我们有一个水果类 /** * 水果类 */ public abstract

JAVA-初步认识-常用对象API(集合框架-泛型-泛型限定-上限的体现)

一. 现在要说应用,查看一下API中的注释, 这里有一个方法addAll里面有extedns E,我们看一下这个方法什么时候用.下面这三个集合,有存储person,student,还有worker的. 再接下来演示一下这个高级应用,如果我们要去想定义一下集合的话,(描述一下集合) 这个类里面加入的元素不确定的情况下,写的是个E,明确什么类型,add方法中添加的也是什么类型.一次添加一个元素比较慢,addAll方法可以添加一堆元素, 一般情况下,就是这么写.(addAll又将mycollectio

有所述网络流的上限和下限

从 http://www.cnblogs.com/kane0526/archive/2013/04/05/3001108.html 版权声明:本文博客原创文章.博客,未经同意,不得转载.

JS,上限,下限,随机数

var random=function(up,down){ return Math.random()*(up-down+1)+down; } //如果需要100-200的随机数 random(200,100);

max_element 返回值上限与下限

#include <iostream> #include <algorithm> #include <list> using namespace std; //using namespace std::list; //using namespace std::cout; //using namespace std::endl; int main(int argc, char** argv) { typedef list<int> IntList; IntLi