public interface HasColor {
public void getColor();
}
class Colored{
T t;
Colored(T t){
this.t=t;
}
T getT(){
return t;
}
//界限允许您调用方法
void color(){
t.getColor();
}
}
class Dimension{
public int x,y,z;
}
class ColoredDimension{
T t;
public ColoredDimension(T t) {
this.t = t;
}
public T getT() {
return t;
}
void color(){
t.getColor();
}
int getX(){
return t.x;
}
int getY(){
return t.y;
}
int getZ(){
return t.z;
}
}
interface Weight{
int weight();
}
class Solid{
T t;
public Solid(T t) {
this.t = t;
}
public T getT() {
return t;
}
void color(){
t.getColor();
}
int getX(){
return t.x;
}
int getY(){
return t.y;
}
int getZ(){
return t.z;
}
int weight(){
return t.weight();
}
}
class Bounded extends Dimension implements HasColor,Weight{
@Override
public void getColor() {
System.out.println("Bounded getColor method");
}
@Override
public int weight() {
return 0;
}
}
class BasicBounds{
public static void main(String[] args) {
Solid solid=new Solid<>(new Bounded());
solid.color();
solid.getX();
solid.weight();
}
}
public class HoldItem {
T t;
public HoldItem(T t) {
this.t = t;
}
T getT(){
return t;
}
}
class Colored2 extends HoldItem{
public Colored2(T t) {
super(t);
}
void color(){
t.getColor();
}
}
class ColoredDimesion2 extends Colored2{
public ColoredDimesion2(T t) {
super(t);
}
int getX(){
return t.x;
}
int getY(){
return t.y;
}
int getZ(){
return t.z;
}
}
class Solid2 extends ColoredDimesion2{
public Solid2(T t) {
super(t);
}
int weight(){
return t.weight();
}
}
class InheritBounds{
public static void main(String[] args) {
Solid2 solid2=new Solid2<>(new Bounded());
solid2.color();
solid2.getX();
solid2.weight();
}
}
public interface SuperPower {}
interface XRayVision extends SuperPower{
void seeThroughWalls();
}
interface SuperHearing extends SuperPower{
void hearThroughNoises();
}
interface SuperSmell extends SuperPower{
void trackBySmell();
}
class SuperHerp{
T t;
public SuperHerp(T t) {
this.t = t;
}
public T getT() {
return t;
}
}
class SuperSleuth extends SuperHerp {
public SuperSleuth(T t) {
super(t);
}
void see(){
t.seeThroughWalls();
}
}
class CanineHero extends SuperHerp{
public CanineHero(T t) {
super(t);
}
void hear(){
t.hearThroughNoises();
}
void smell(){
t.trackBySmell();
}
}
class SuperHearSmell implements SuperHearing,SuperSmell{
@Override
public void hearThroughNoises() {}
@Override
public void trackBySmell() {}
}
class DogBoy extends CanineHero{
public DogBoy() {
super(new SuperHearSmell());
}
}
class EpicBattle{
//bounds in generic methods 泛型方法的界限
static void useSuperHearing(SuperHerp tSuperHerp){
tSuperHerp.getT().hearThroughNoises();
}
static void superFind(SuperHerp superHerp){
superHerp.getT().hearThroughNoises();
superHerp.getT().trackBySmell();
}
public static void main(String[] args) {
DogBoy dogBoy=new DogBoy();
useSuperHearing(dogBoy);
superFind(dogBoy);
//you can do this 你可以这样做
//List extends SuperHearing> audioBoys
//But you can't do this 但是你做不到
//List extends SuperHearing & SuperSmell> dogBoys
}
}
public class Fruit {
}
class Apple extends Fruit {
}
class Jonathan extends Apple {
}
class Orange extends Fruit {
}
class CovariantArrays {
public static void main(String[] args) {
Fruit[] fruits = new Apple[10];
fruits[0] = new Apple();
fruits[1] = new Jonathan();
//运行时类型是 apple[] 而不是 Fruit[] 或 Orange
try {
//编译器允许您添加 Fruit
//ArrayStoreException 异常
fruits[2] = new Fruit();
} catch (Exception e) {
System.err.println(e);
}
try {
//编译器不允许你添加 Orange
//ArrayStoreException 异常
fruits[3] = new Orange();
}catch (Exception e){
System.err.println(e);
}
}
}
//运行结果为
java.lang.ArrayStoreException: generic.Fruit
java.lang.ArrayStoreException: generic.Orange
class NonCovariantGenerics{
//编译异常 不兼容的类型
List fruitList=new ArrayList();
}
class GenericAndGCovariange{
public static void main(String[] args) {
//wildcards allow covariance 通配符允许协方差
List extends Fruit> list=new ArrayList();
//编译错误:无法添加任何类型的对象
//list.add(new Apple());
//list.add(new Orange());
//list.add(new Fruit());
//合法但无趣
list.add(null);
//我们知道它至少返回 fruit
Fruit fruit = list.get(0);
}
}
编译器有多聪明
class CompilerIntelligence{
public static void main(String[] args) {
List extends Fruit> list= Arrays.asList(new Apple());
//没有警告
Apple apple= (Apple) list.get(0);
//Argument is Object
list.contains(new Apple());
list.indexOf(new Apple());
}
}
public class Holder {
private T t;
public Holder(T t) {
this.t = t;
}
public Holder() {
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
@Override
public boolean equals(Object object) {
return t.equals(object);
}
public static void main(String[] args) {
Holder holder = new Holder<>(new Apple());
Apple apple = holder.getT();
holder.setT(apple);
//Holder fruitHolder=holder; 无法向上转型
Holder extends Fruit> fruit = holder;
Fruit fruit1 = fruit.getT();
//返回的结果是 object
apple= (Apple) fruit.getT();
try {
Orange orange = (Orange) fruit.getT();
}catch (Exception e){
System.out.println(e);
}
//fruit.setT(new Apple());
//fruit.setT(new Fruit());
System.out.println(fruit.equals(apple));
}
}
//运行结果为
java.lang.ClassCastException: generic.Apple cannot be cast to generic.Orange
true
public class SuperTypeWildcards {
public static void main(String[] args) {
List super Apple> apples=new ArrayList<>();
apples.add(new Apple());
apples.add(new Jonathan());
//apples.add(new Fruit()); 编译失败
}
}
public class GenericWriting {
static List apples=new ArrayList<>();
static List fruits=new ArrayList<>();
static void f1(){
writeExact(apples,new Apple());
writeExact(fruits,new Apple());
}
static void writeWithWildcard(List super T> list,T item){
list.add(item);
}
static void writeExact(List list,T item){
list.add(item);
}
static void f2(){
writeWithWildcard(apples,new Apple());
writeWithWildcard(fruits,new Apple());
}
public static void main(String[] args) {
f1();
f2();
}
}
public class GenericReading {
static T readExact(List list) {
return list.get(0);
}
static List apples = Arrays.asList(new Apple());
static List fruits = Arrays.asList(new Fruit());
//静态方法适应每个调用
static void f1() {
Apple apple = readExact(apples);
Fruit fruit = readExact(fruits);
fruit = readExact(apples);
}
//如果您有一个类,那么它的类型是在实例化类时建立
static class Reader {
T readExact(List list) {
return list.get(0);
}
}
static void f2() {
Reader reader = new Reader<>();
Fruit fruit = reader.readExact(fruits);
// reader.readExact(apples); 错误
//readExact(List) 不可能是 applied to (List)
}
static class CovariantReader{
T readCovariant(List extends T> list){
return list.get(0);
}
}
static void f3(){
CovariantReader covariantReader=new CovariantReader<>();
Fruit fruit = covariantReader.readCovariant(fruits);
Fruit fruit1 = covariantReader.readCovariant(apples);
}
public static void main(String[] args) {
f1();
f2();
f3();
}
}
无界通配符> 看起来意味着任何事物,因此使用无界通配符好像等价于使用原生类型。事实上,编译器初看起来是支持这种判断的。
public class UnboundedWildcards1 {
static List list1;
static List> list2;
static List extends Object> list3;
static void assign1(List list) {
list1 = list;
list2 = list;
list3 = list;
}
static void assign2(List> list) {
list1 = list;
list2 = list;
list3 = list;
}
static void assign3(List extends Object> list) {
list1 = list;
list2 = list;
list3 = list;
}
public static void main(String[] args) {
assign1(new ArrayList());
assign2(new ArrayList());
assign3(new ArrayList());
assign1(new ArrayList());
assign2(new ArrayList());
assign3(new ArrayList());
List> lists = new ArrayList<>();
lists=new ArrayList();
assign1(lists);
assign2(lists);
assign3(lists);
}
}
public class UnboundedWildcards {
static Map map;
static Map,?> map1;
static Map map2;
static void assign1(Map map){
map=map;
}
static void assign2(Map,?> map){
map1=map;
}
static void assign3(Map map){
map2=map;
}
public static void main(String[] args) {
assign1(new HashMap());
assign2(new HashMap());
assign3(new HashMap());
assign1(new HashMap());
assign2(new HashMap());
assign3(new HashMap());
}
}