泛型的上限和下限的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;
}
}
泛型的上限和下限的Demo的更多相关文章
- java 泛型的上限与下限
设置泛型对象的上限使用extends,表示参数类型只能是该类型或该类型的子类: 声明对象:类名<? extends 类> 对象名 定义类:类名<泛型标签 extends 类>{ ...
- java中泛型上限,下限应用
v 一.程序中无形之中用到的泛型 import java.util.*; class Person implements Comparable<Person>{ String name; ...
- Java笔记2 : 泛型的体现,及其上限、下限、通配符
Java中的泛型是在jdk5.0引入的,语法不难,但是需要注意的细节有很多,这里写一下备忘. 首先是最简单的泛型类,泛型方法,泛型接口: //泛型接口的定义 interface MyInter< ...
- 牛客网Java刷题知识点之泛型概念的提出、什么是泛型、泛型在集合中的应用、泛型类、泛型方法、泛型接口、泛型限定上限、泛型限定下限、 什么时候使用上限?泛型限定通配符的体现
不多说,直接上干货! 先来看个泛型概念提出的背景的例子. GenericDemo.java package zhouls.bigdata.DataFeatureSelection; import ja ...
- Java泛型-通配符的上限和下限问题
Java的泛型中,通配符可以设置上限和下限. 上限:<? extends T> ?是T和T的子类 下限:<? super T> ?是T和T的父类 怎么看待这个上限和下限呢 首先 ...
- Java的泛型中,通配符可以设置上限和下限
上限:<? extends T> ?是T和T的子类 下限:<? super T> ?是T和T的父类 怎么看待这个上限和下限呢 首先应该想 其实对于Java来说 <? ex ...
- WInform中实现设置ZedGraph中曲线的X轴与Y轴的上限与下限
场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
- Java 泛型通配符上限和通配符下限
①为什么要使用泛型通配符 请参考这篇随笔的下半部分 https://www.cnblogs.com/baxianhua/p/9194369.html ②通配符上限和通配符下限的简单解释 <? ...
- js生成一个上限跟下限的随机数
function sj() { //x上限,y下限 var x = 2000; var y = 1800; var rand = parseInt(Math.random() * (x - y + 1 ...
随机推荐
- bootstrap-6
表格:bootstrap为表格提供了1种基础样式和4种附加样式以及一个支持响应式的表格.主要包括: .table:基础表格 .table-striped:斑马线表格 .table-bordered:带 ...
- Alpha版本项目展示
成员简介 谷大鑫: 热爱编程,技术狂魔,可以对感兴趣的技术钻研到茶饭不思,队伍的技术中坚.标签:整个队伍里最靠谱的人. 个人博客:http://www.cnblogs.com/nrm1/ 杨金键: 未 ...
- Github 笔记
在本地创建并切换 git checkout -b your_branch_name 把本地分支的修改提交到远端的分支上 git push origin local_branch_name:remote ...
- SpringMVC——form标签的使用
----------------------------------------------------------------------------------- <%@page impor ...
- QTreeWidget创建
QTreeWidget.顾名思义,这个类用来展示树型结构.同前面说的QListWidget类似,这个类需要同另外一个辅助类QTreeWidgetItem一同使用.不过,既然是提供方面的封装类,即便是看 ...
- Content is not allowed in prolog.解决方法
将xml配置文件利用记事本另存为Anis编码的文件可以解决.
- 墨菲定律-Murphy's Law (转载)
墨菲定律 “墨菲定律”(Murphy's Law)亦称莫非定律.莫非定理.或摩菲定理,是西方世界常用的俚语. “墨菲定律”:事情往往会向你所想到的不好的方向发展,只要有这个可能性.比如你衣袋里有两把钥 ...
- python学习-day18、文件处理、
4.文件操作 武sir:http://www.cnblogs.com/wupeiqi/articles/4943406.html 林海峰:http://www.cnblogs.com/linhaife ...
- HTML标签_01
<!DOCTYPE html> <html> <body> <h1>我的第一个标题</h1> <p>我的第一个段落.</p ...
- Android学习笔记(八)
android中常见空间的使用方法 1.TextView TextView主要用于在界面上显示一段文本信息,如下面代码所示: <LinearLayout xmlns:android=" ...