image-ru-00-21

「哇,又來一個人類女性。但這次是黑頭髮。真開心!」

「嗨,我叫金。」

「嗨,我叫阿米哥。」

「我知道。你的名字是我想到的。迪亞哥想不出這種好名字。」

阿米哥的思緒以電子奔騰的速度馳騁著。「嗯…她人好好喔…不知道她喜不喜歡機器人。」

「讓我們回到課堂吧。我會用簡單的方式把內容解釋給你聽。」

「OK。」

「我想幫教授和利施說過的內容加上說明。」

「在 Java 裡,你可以寫命令,你也可以直接在代碼裡那些命令旁邊加上註解 (comment)。編譯器會完全忽略那些註解。程式運行的時候,所有的註解都會被忽略。

「可以請你給我一個範例嗎?」

「噢,當然:」

public class Home
{
    public static void main(String[] args)
    {
        /*
        Now we'll display the phrase 'Amigo Is The Best' on the screen
        */
        System.out.print("Amigo ");
        System.out.print("Is ");
        System.out.print("The ");
        System.out.print("Best");
    }
}    

「我們加了『現在我們要把…』這個註解。註解的開頭用一個符號對 (/*) 標示,結尾則是用 (*/) 標示。程式被編譯時,編譯器會忽略在符號 /**/ 中間的所有東西。

「也就是說我可以愛寫什麼就寫什麼?」

「是的。一般來說,代碼裡的註解是有關代碼中比較難懂的部分。有些註解是由好幾個字串構成的,通常寫在方法之前,以描述方法運作的原理細節。」

「要在代碼裡加上註解還有一種方法。你可以使用兩個前斜線 (//)。」

public class Home
{
    public static void main(String[] args)
    {
        System.out.print("Amigo ");
        System.out.print("Is The "); // This is also a comment
        System.out.print("Best");
    }
}

「在這裡,// 開始一直到有 // 符號的同一行結束前的代碼,都被視為註解。也就是說,沒有第二個符號對來『完成註解』。」

「順帶一提,有些註解真的很有趣。」

// I'm not responsible of this code. They made me write it, against my will.
// Dear future me. Please forgive me.
// I can't even begin to express how sorry I am.
// If I see something like this once more, I'll have a complete mental breakdown at work.
// If this condition is ever satisfied,
// please inform me for a reward. Phone: xxx-xxx-xxx.
// Dear maintainer:
// Once you are done trying to 'optimize' this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
// total_hours_wasted_here = 42
// When I wrote this, only God and I understood what I was doing
// Now, God only knows
// Sometimes I believe compiler ignores all my comments.
// I dedicate all this code, all my work, to my wife, Darlene, who will 
// have to support me and our three children and the dog once it gets 
// released into the public.
// Drunk, fix later
// Magic. Do not touch

「真的耶,有些註解超好笑。」

「今天就到這裡吧。」

「真是一堂簡短但有趣的課。謝謝妳,金。」