V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sandao
V2EX  ›  Java

吐槽 你们知道土拨鼠的梗嘛?

  •  
  •   sandao · 2018-01-29 13:32:13 +08:00 · 10562 次点击
    这是一个创建于 2250 天前的主题,其中的信息可能已经有所发展或是发生改变。

    《 Thinking in Java 》的 Containers in Depth 一章的 Hashing and hash codes 有这么一段演示代码: 一个天气系统,将 Groundhog (土拨鼠)对象和 Prediction (预报)对象联系起来。

    代码本身很简单,但是看的时候是不是一脸懵逼?ヽ( ̄д ̄;)ノ为什么土拨鼠和天气预报有关?

    土拨鼠日(英语:Groundhog Day ),是北美地区的一个传统节日。每年 2 月 2 日,美国和加拿大许多城市和村庄都会庆祝。自从 1887 年以来,一代又一代的土拨鼠一直担负着预报时令的任务。根据传说,如果土拨鼠能看到它自己的影子,那么北美的冬天还有 6 个星期才会结束。如果它看不到影子,春天不久就会来临。-----摘自维基百科

    俏丽奶奶ಥ_ಥ

    最后推荐下《土拨鼠之日》这部电影(´°ᗜ°)ハハッ

    //: containers/Groundhog.java
        // Looks plausible, but doesn ’ t work as a HashMap key.
        public class Groundhog {
          protected int number;
          public Groundhog(int n) { number = n; }
          public String toString() {
            return "Groundhog #" + number;
          }
    } ///:~
    //: containers/Prediction.java
        // Predicting the weather with groundhogs.
        import java.util.*;
        public class Prediction {
          private static Random rand = new Random(47);
          private boolean shadow = rand.nextDouble() > 0.5;
          public String toString() {
            if(shadow)
              return "Six more weeks of Winter!";
            else
              return "Early Spring!";
    }
    } ///:~
    //: containers/SpringDetector.java
        // What will the weather be?
        import java.lang.reflect.*;
        import java.util.*;
        import static net.mindview.util.Print.*;
        public class SpringDetector {
          // Uses a Groundhog or class derived from Groundhog:
          public static <T extends Groundhog>
          void detectSpring(Class<T> type) throws Exception {
            Constructor<T> ghog = type.getConstructor(int.class);
            Map<Groundhog,Prediction> map =
              new HashMap<Groundhog,Prediction>();
            for(int i = 0; i < 10; i++)
              map.put(ghog.newInstance(i), new Prediction());
            print("map = " + map);
            Groundhog gh = ghog.newInstance(3);
            print("Looking up prediction for " + gh);
            if(map.containsKey(gh))
              print(map.get(gh));
            else
              print("Key not found: " + gh);
          }
          public static void main(String[] args) throws Exception {
            detectSpring(Groundhog.class);
    
    20 条回复    2018-01-29 18:22:16 +08:00
    anguslg
        1
    anguslg  
       2018-01-29 13:37:11 +08:00
    我是在看完<源代码>和<明日边缘>之后看的<土拨鼠之日>, 看到你的标题我第一想到的就是这个电影
    taisenjay
        2
    taisenjay  
       2018-01-29 13:44:57 +08:00
    涨了一波知识,666
    sandao
        3
    sandao  
    OP
       2018-01-29 13:49:34 +08:00 via Android
    @anguslg 我也是诶,源代码我记得我还是高中跑去学校一个人把投影仪打开看的,当时觉得除了男演员其他演员都好省事(´°ᗜ°)ハハッ
    jerry12547
        4
    jerry12547  
       2018-01-29 14:00:43 +08:00
    这个前几天我在知乎上看到过,他们还养着那个土拨鼠来着,然后市长还会过来庆祝。。
    pusidun
        5
    pusidun  
       2018-01-29 15:06:04 +08:00
    不是说,有一次土拨鼠预测错了,还被起诉了?
    QAPTEAWH
        6
    QAPTEAWH  
       2018-01-29 15:07:57 +08:00   ❤️ 1
    土拨鼠:啊~~~~~~
    imn1
        7
    imn1  
       2018-01-29 15:11:19 +08:00
    土拨鼠:None of my businese !
    sandao
        8
    sandao  
    OP
       2018-01-29 15:13:37 +08:00 via Android
    @pusidun 是的,心疼无辜的土拨鼠Ծ ̮ Ծ
    ReVanTis
        9
    ReVanTis  
       2018-01-29 15:15:10 +08:00
    土拨鼠发来贺电
    sandao
        10
    sandao  
    OP
       2018-01-29 15:15:19 +08:00 via Android
    @QAPTEAWH 画面感很强(´°ᗜ°)ハハッ
    oswuhan
        11
    oswuhan  
       2018-01-29 15:17:19 +08:00
    @QAPTEAWH #6 第一时间想起来的就是这个梗
    paloalto
        12
    paloalto  
       2018-01-29 15:23:40 +08:00   ❤️ 2
    wyntalgeer
        13
    wyntalgeer  
       2018-01-29 15:38:41 +08:00
    不是报错了一次判了死刑
    tedzhou1221
        14
    tedzhou1221  
       2018-01-29 15:59:24 +08:00
    @wyntalgeer 后来好像土拨鼠的支持者提起上诉了!哈哈
    SingeeKing
        15
    SingeeKing  
       2018-01-29 16:04:12 +08:00
    土拨鼠…… 我前女友外号。。
    MontagePa
        16
    MontagePa  
       2018-01-29 16:49:45 +08:00
    问题是,来年春天发现土拨鼠,会顺带把他们吃掉啊。
    sandao
        17
    sandao  
    OP
       2018-01-29 16:59:07 +08:00 via Android
    @MontagePa 能怎好?噢,不,好吃吗?
    boywang004
        18
    boywang004  
       2018-01-29 17:05:44 +08:00
    这个梗的确之前不知道……好吧。
    Anybfans
        19
    Anybfans  
       2018-01-29 17:36:08 +08:00   ❤️ 1
    !()[ ]
    roricon
        20
    roricon  
       2018-01-29 18:22:16 +08:00
    @paloalto 我首先想到的也是这个图.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1478 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:50 · PVG 07:50 · LAX 16:50 · JFK 19:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.