badapple.java

来自「java设计模式源码」· Java 代码 · 共 47 行

JAVA
47
字号
package app.proxy.dynamic;

/**
 * An example of a class with a method that takes too long to execute.
 */
public class BadApple {
    public String name;

    /**
     * Create a "bad apple" with the given name.
     * @param name
     */
    public BadApple(String name) {
        this.name = name;
    }

    /**
     * This is here just to show a class that implements behaviors for
     * participation in sets.
     */
    public boolean equals(Object o) {
        if (!(o instanceof BadApple))
            return false;
        BadApple f = (BadApple) o;
        return name.equals(f.name);
    }

    /**
     * This is the "bad" code. It takes a nap so the routine takes artificially
     * long to run.
     */
    public int hashCode() {
        try {
            Thread.sleep(1200);
        } catch (InterruptedException ignored) {
        }
        return name.hashCode();
    }

    /**
     * Provide a string representation of this object.
     */
    public String toString() {
        return name;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?