java 通配符使用_java 通配符的使用

简介

java 通配符 是java写库的人必备的技能树

感觉有点模糊

jdk 版本要求较高 否则可能出现变异不通过的情况 最好用最新版本(jdk14) 虽然java 核心编程要求的是jdk1.8 但是我的jdk8 是编译不过去的

code

package com.company;

import com.company.Manager;

public class PairTest3

{

public static void main(String[] args)

{

Manager ceo = new Manager("Gus Greedy", 800000, 2003, 12, 15);

Manager cfo = new Manager("Sid Sneaky", 600000, 2003, 12, 15);

Pair buddies = new Pair(ceo, cfo);

printBuddies(buddies);

ceo.setBonus(1000000);

cfo.setBonus(500000);

Manager[] managers = { ceo, cfo };

Pair result = new Pair();

minmaxBonus(managers, result);

System.out.println("first: " + result.getFirst().getName()

+ ", second: " + result.getSecond().getName());

maxminBonus(managers, result);

System.out.println("first: " + result.getFirst().getName()

+ ", second: " + result.getSecond().getName());

}

public static void printBuddies(Pair extends Employee> p)

{

Employee first = p.getFirst();

Employee second = p.getSecond();

System.out.println(first.getName() + " and " + second.getName() + " are buddies.");

}

public static void minmaxBonus(Manager[] a, Pair super Manager> result)

{

if (a == null || a.length == 0) return;

Manager min = a[0];

Manager max = a[0];

for (int i = 1; i < a.length; i++)

{

if (min.getBonus() > a[i].getBonus()) min = a[i];

if (max.getBonus() < a[i].getBonus()) max = a[i];

}

result.setFirst(min);

result.setSecond(max);

}

public static void maxminBonus(Manager[] a, Pair super Manager> result)

{

minmaxBonus(a, result);

PairAlg.swapHelper(result); // OK--swapHelper captures wildcard type

}

}

class PairAlg

{

public static boolean hasNulls(Pair> p)

{

return p.getFirst() == null || p.getSecond() == null;

}

public static void swap(Pair> p) { swapHelper(p); }

public static void swapHelper(Pair p)

{

T t = p.getFirst();

p.setFirst(p.getSecond());

p.setSecond(t);

}

}

package com.company;

public class Manager extends Employee{

private double bonus;

public Manager(String name, double salary, int year, int month, int day) {

super(name, salary, year, month, day);

bonus = 0;

}

public double getSalary() {

double baseSalary = super.getSalary();

return baseSalary + bonus;

}

public void setBonus(double bonus) {

this.bonus = bonus;

}

public boolean equals(Object otherObject){

if (!super.equals(otherObject)) return false;

Manager other = (Manager) otherObject;

return bonus == other.bonus;

}

public int hashCode(){

return super.hashCode() + 17 * new Double(bonus).hashCode();

}

public String toString() {

return super.toString() + "[bonus=" + bonus + "]";

}

public double getBonus() {

// TODO Auto-generated method stub

return this.bonus;

}

}

package com.company;

public class Pair

{

public Pair() { first = null; second = null; }

public Pair(T first, T second) { this.first = first; this.second = second; }

public T getFirst() { return first; }

public T getSecond() { return second; }

public void setFirst(T newValue) { first = newValue; }

public void setSecond(T newValue) { second = newValue; }

private T first;

private T second;

}

package com.company;

import java.time.*;

import java.util.Objects;

public class Employee{

private String name;

private double salary;

private LocalDate hireDay;

public Employee(String name, double salary, int year, int month, int day){

this.name = name;

this.salary = salary;

hireDay = LocalDate.of(year, month, day);

}

public String getName() {

return name;

}

public double getSalary() {

return salary;

}

public LocalDate getHireDay() {

return hireDay;

}

public String getDescription() {

return String.format("an emplyee with a salary of $%.2f", salary);

}

public void raiseSalary(double byPercent) {

double raise = salary * byPercent / 100;

salary += raise;

}

public boolean equals(Object otherObject) {

if (this == otherObject) return true;

if (otherObject == null) return false;

if (getClass() != otherObject.getClass()) return false;

Employee other = (Employee) otherObject;

return Objects.equals(name, other.name) && salary == other.salary

&& Objects.equals(hireDay, other.hireDay);

}

public int hashCode(){

return Objects.hash(name, salary, hireDay);

}

public String toString() {

return getClass().getName() + "[name=" + name + ",salary=" + salary + ",hireDay=" + hireDay + "]";

}

}

Q&A

? super Manager 的作用 extends Employee>

带有超类型限定的通配符可以向范型对象写入,带有子类型限定的通配符可以从泛型对象读取。

Tips

初看有点不懂,还是等以后工作的时候一边写代码一边熟悉。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值