java泛型边界深入探讨,示例代码
package org.rui.generics.bounds; import java.awt.Color; /** * 边界 * 下面展示了边界基本要素。 * @author lenovo * */ interface HasColor{java.awt.Color getColor();} class Colored<T extends HasColor> { T item; Colored(T item){this.item=item;} T getItem(){return item;} java.awt.Color color(){return item.getColor();} } /////////////////////////////////////////////////// class Dimensio{public int x,y,z;}//尺码 //!class ColoredDimension<T extends HasColor & Dimensio> class ColoredDimension<T extends Dimensio & HasColor> { T item; ColoredDimension(T item){this.item=item;} T getItem(){return item;} java.awt.Color color(){return item.getColor();} int getX(){return item.x;} int getY(){return item.y;} int getZ(){return item.z;} } ///////////////立方体/////////////////////////////////////////////// interface Weight{int weight();} class Solid<T extends Dimensio & HasColor & Weight> { T item; Solid(T item){this.item=item;} T getItem(){return item;} java.awt.Color color(){return item.getColor();} int getX(){return item.x;} int getY(){return item.y;} int getZ(){return item.z;} // int getWeight(){return item.weight();} } ////////////////////////////////////////////////////////////////////// class Bounded extends Dimensio implements HasColor,Weight { public int weight(){return 0;} public Color getColor() {return null;} } ////////////////////////////////////////////////////////////////////////// public class BasicBounds { public static void main(String[] args) { Solid<Bounded> solid=new Solid<Bounded>(new Bounded()); solid.color(); solid.getY(); solid.getWeight(); } }
package org.rui.generics.bounds; import java.awt.Color; /** * 边界 * 看上去包含可以通过继承消除沉余。 * 下面可以看到在继承的每个层次上添加边界的限制 * @author lenovo * */ class holdItem<T> { T item; holdItem(T item){this.item=item;} T getItem(){return item;} } interface HasColor2{java.awt.Color getColor();} class Colored2<T extends HasColor2> extends holdItem<T> { //T item; Colored2(T item){super(item);} //T getItem(){return item;} java.awt.Color color(){return item.getColor();} } /////////////////////////////////////////////////// class Dimensio2{public int x,y,z;}//尺码 class ColoredDimension2<T extends Dimensio2 & HasColor2> extends Colored2<T> { ColoredDimension2(T item){super(item);} int getX(){return item.x;} int getY(){return item.y;} int getZ(){return item.z;} } ///////////////立方体/////////////////////////////////////////////// interface Weight2{int weight();} class Solid2<T extends Dimensio2 & HasColor2 & Weight2> extends ColoredDimension2<T> { Solid2(T item){super(item);} int getWeight(){return item.weight();} } ////////////////////////////////////////////////////////////////////// class Bounded2 extends Dimensio2 implements HasColor2,Weight2 { public int weight(){return 0;} public Color getColor() {return null;} } ////////////////////////////////////////////////////////////////////////// public class InheritBounds { public static void main(String[] args) { Solid2<Bounded2> solid=new Solid2<Bounded2>(new Bounded2()); solid.color(); solid.getY(); solid.getWeight(); } }
package org.rui.generics.bounds; import java.awt.Color; import java.util.List; /** * 边界 * 多层次的示例 * @author lenovo * */ interface SuperPower{} //视觉 interface XRayVision extends SuperPower { void seeThroughWalls(); } //听力 interface SuperHearing extends SuperPower { void hearSubtleNoises(); } //闻 interface SuperSmell extends SuperPower { void trackBysmell(); } //////////英雄;男主角//////////////////////////////// class SuperHero<POWER extends SuperPower> { POWER power; SuperHero(POWER power){this.power=power;} POWER getPower(){return power;} } //////////侦查//////////////////////////////// class SuperSleuth<POWER extends XRayVision> extends SuperHero<POWER> { SuperSleuth(POWER power){super(power);} void see(){power.seeThroughWalls();} } ////////////犬////////////////////////////// class CanineHero<POWER extends SuperHearing & SuperSmell> extends SuperHero<POWER> { CanineHero(POWER power){super(power);} void hear(){power.hearSubtleNoises();} void smell(){power.trackBysmell();} } /////////////////////////////////////// class SuperHearSmell implements SuperHearing,SuperSmell { @Override public void trackBysmell() {} @Override public void hearSubtleNoises() {} } /////////////////////////////////////// class DogBoy extends CanineHero<SuperHearSmell> { DogBoy() {super(new SuperHearSmell());} } ////////////////////////////////////////////////////////////////////////// public class EpicBattle { //bounds in generic methods static <POWER extends SuperHearing> void userSuperHearing(SuperHero<POWER> hero ) { hero.getPower().hearSubtleNoises(); } static <POWER extends SuperHearing & SuperSmell> void sperFind(SuperHero<POWER> hero) { hero.getPower().hearSubtleNoises(); hero.getPower().trackBysmell(); } public static void main(String[] args) { DogBoy dogboy=new DogBoy(); userSuperHearing(dogboy); sperFind(dogboy); //YOU can do this List<? extends SuperHearing> audioBoys; //but you can 't do this: //List<? extends SuperHearing & SuperSemll> audioBoys; } }