本文共 2069 字,大约阅读时间需要 6 分钟。
/** * 作者:张风捷特烈 * 时间:2018/8/25 0025:9:23 * 邮箱:1981462002@qq.com * 说明:游戏王游戏抽象类 */public abstract class YoGIOhGame { public final void play() { shuffle(); draw(); run(); if (isWin()) { win(); } else { lost(); } } private void shuffle () { System.out.println("洗牌"); } private void draw () { System.out.println("抽牌"); } private void win () { System.out.println("赢"); } private void lost () { System.out.println("输"); } protected boolean isWin() { return true; } abstract void run();}
public class Player { public static void main(String[] args) { new YoGIOhGame() { @Override void run() { System.out.println("奥西里斯的天空龙直接攻击玩家!"); } }.play(); }}
洗牌抽牌奥西里斯的天空龙直接攻击玩家!赢
public class Player { public static void main(String[] args) { new YoGIOhGame() { @Override void run() { System.out.println("奥西里斯的天空龙直接攻击玩家!"); } @Override protected boolean isWin() { return false; } }.play(); }}
/** * 作者:张风捷特烈 * 时间:2018/8/25 0025:10:16 * 邮箱:1981462002@qq.com * 说明:耗时测试类 */public abstract class TimeTest { public TimeTest() { this(""); } public TimeTest(String str) { long startTime = System.currentTimeMillis(); run(); long endTime = System.currentTimeMillis(); System.out.println(str+"方法耗时:" + (endTime - startTime)/1000.f + "秒"); } protected abstract void run();}
public static void main(String[] args) { new TimeTest("run") { @Override protected void run() { for (int i = 0; i < 100000; i++) { System.out.println("hh"); } } }; }
本文由张风捷特烈原创,转载请注明