// generics/BasicBounds.java// (c)2017 MindView LLC: see Copyright.txt// We make no guarantees that this code is fit for any purpose.// Visit http://OnJava8.com for more book information.interface HasColor { java.awt.Color getColor(); }class WithColor<T extends HasColor> {T item;WithColor(T item) { this.item = item; }T getItem() { return item; }// The bound allows you to call a method:java.awt.Color color() { return item.getColor(); }}class Coord { public int x, y, z; }// This fails. Class must be first, then interfaces:// class WithColorCoord<T extends HasColor & Coord> {// Multiple bounds:class WithColorCoord<T extends Coord & HasColor> {T item;WithColorCoord(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(); }// As with inheritance, you can have only one// concrete class but multiple interfaces:class Solid<T extends Coord & 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 weight() { return item.weight(); }}class Boundedextends Coord implements HasColor, Weight {@Overridepublic java.awt.Color getColor() { return null; }@Overridepublic int weight() { return 0; }}public class BasicBounds {public static void main(String[] args) {Solid<Bounded> solid =new Solid<>(new Bounded());solid.color();solid.getY();solid.weight();}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。