<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### [標記接口](https://lingcoder.gitee.io/onjava8/#/book/19-Type-Information?id=%e6%a0%87%e8%ae%b0%e6%8e%a5%e5%8f%a3) 有時候使用一個**標記接口**來表示空值會更方便。標記接口里邊什么都沒有,你只要把它的名字當做標簽來用就可以。 ~~~ // onjava/Null.java package onjava; public interface Null {} ~~~ 如果你用接口取代具體類,那么就可以使用`DynamicProxy`來自動地創建`Null`對象。假設我們有一個`Robot`接口,它定義了一個名字、一個模型和一個描述`Robot`行為能力的`List<Operation>`: ~~~ // typeinfo/Robot.java import onjava.*; import java.util.*; public interface Robot { String name(); String model(); List<Operation> operations(); static void test(Robot r) { if (r instanceof Null) System.out.println("[Null Robot]"); System.out.println("Robot name: " + r.name()); System.out.println("Robot model: " + r.model()); for (Operation operation : r.operations()) { System.out.println(operation.description.get()); operation.command.run(); } } } ~~~ 你可以通過調用`operations()`來訪問`Robot`的服務。`Robot`里邊還有一個`static`方法來執行測試。 `Operation`包含一個描述和一個命令(這用到了**命令模式**)。它們被定義成函數式接口的引用,所以可以把 lambda 表達式或者方法的引用傳給`Operation`的構造器: ~~~ // typeinfo/Operation.java import java.util.function.*; public class Operation { public final Supplier<String> description; public final Runnable command; public Operation(Supplier<String> descr, Runnable cmd) { description = descr; command = cmd; } } ~~~ 現在我們可以創建一個掃雪`Robot`: ~~~ // typeinfo/SnowRemovalRobot.java import java.util.*; public class SnowRemovalRobot implements Robot { private String name; public SnowRemovalRobot(String name) { this.name = name; } @Override public String name() { return name; } @Override public String model() { return "SnowBot Series 11"; } private List<Operation> ops = Arrays.asList( new Operation( () -> name + " can shovel snow", () -> System.out.println( name + " shoveling snow")), new Operation( () -> name + " can chip ice", () -> System.out.println(name + " chipping ice")), new Operation( () -> name + " can clear the roof", () -> System.out.println( name + " clearing roof"))); public List<Operation> operations() { return ops; } public static void main(String[] args) { Robot.test(new SnowRemovalRobot("Slusher")); } } ~~~ 輸出結果: ~~~ Robot name: Slusher Robot model: SnowBot Series 11 Slusher can shovel snow Slusher shoveling snow Slusher can chip ice Slusher chipping ice Slusher can clear the roof Slusher clearing roof ~~~ 假設存在許多不同類型的`Robot`,我們想讓每種`Robot`都創建一個`Null`對象來執行一些特殊的操作——在本例中,即提供`Null`對象所代表`Robot`的確切類型信息。這些信息是通過動態代理捕獲的: ~~~ // typeinfo/NullRobot.java // Using a dynamic proxy to create an Optional import java.lang.reflect.*; import java.util.*; import java.util.stream.*; import onjava.*; class NullRobotProxyHandler implements InvocationHandler { private String nullName; private Robot proxied = new NRobot(); NullRobotProxyHandler(Class<? extends Robot> type) { nullName = type.getSimpleName() + " NullRobot"; } private class NRobot implements Null, Robot { @Override public String name() { return nullName; } @Override public String model() { return nullName; } @Override public List<Operation> operations() { return Collections.emptyList(); } } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return method.invoke(proxied, args); } } public class NullRobot { public static Robot newNullRobot(Class<? extends Robot> type) { return (Robot) Proxy.newProxyInstance( NullRobot.class.getClassLoader(), new Class[] { Null.class, Robot.class }, new NullRobotProxyHandler(type)); } public static void main(String[] args) { Stream.of( new SnowRemovalRobot("SnowBee"), newNullRobot(SnowRemovalRobot.class) ).forEach(Robot::test); } } ~~~ 輸出結果: ~~~ Robot name: SnowBee Robot model: SnowBot Series 11 SnowBee can shovel snow SnowBee shoveling snow SnowBee can chip ice SnowBee chipping ice SnowBee can clear the roof SnowBee clearing roof [Null Robot] Robot name: SnowRemovalRobot NullRobot Robot model: SnowRemovalRobot NullRobot ~~~ 無論何時,如果你需要一個空`Robot`對象,只需要調用`newNullRobot()`,并傳遞需要代理的`Robot`的類型。這個代理滿足了`Robot`和`Null`接口的需要,并提供了它所代理的類型的確切名字。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看