<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之旅 廣告
                ## Chapter 4. Classes and Interfaces(類和接口) ### Item 25: Limit source files to a single top-level class(源文件僅限有單個頂層類) While the Java compiler lets you define multiple top-level classes in a single source file, there are no benefits associated with doing so, and there are significant risks. The risks stem from the fact that defining multiple top-level classes in a source file makes it possible to provide multiple definitions for a class. Which definition gets used is affected by the order in which the source files are passed to the compiler. To make this concrete, consider this source file, which contains only a Main class that refers to members of two other top-level classes (Utensil and Dessert): 雖然 Java 編譯器允許你在單個源文件中定義多個頂層類,但這樣做沒有任何好處,而且存在重大風險。這種風險源于這樣一個事實:在源文件中定義多個頂層類使得為一個類提供多個定義成為可能。所使用的定義受源文件傳給編譯器的順序的影響。說得更具體些,請考慮這個源文件,它只包含一個主類,該主類引用另外兩個頂層類的成員(Utensil 和 Dessert): ``` public class Main { public static void main(String[] args) { System.out.println(Utensil.NAME + Dessert.NAME); } } ``` Now suppose you define both Utensil and Dessert in a single source file named Utensil.java: 現在假設你在一個名為 `Utensil.java` 的源文件中定義了 Utensil 類和 Dessert 類: ``` // Two classes defined in one file. Don't ever do this! class Utensil { static final String NAME = "pan"; } class Dessert { static final String NAME = "cake"; } ``` Of course the main program prints pancake. Now suppose you accidentally make another source file named Dessert.java that defines the same two classes: 當然,main 方法應該輸出 pancake。現在假設你意外地制作了另一個名為 Dessert 的源文件。java 定義了相同的兩個類: ``` // Two classes defined in one file. Don't ever do this! class Utensil { static final String NAME = "pot"; } class Dessert { static final String NAME = "pie"; } ``` If you’re lucky enough to compile the program with the command javac Main.java Dessert.java, the compilation will fail, and the compiler will tell you that you’ve multiply defined the classes Utensil and Dessert. This is so because the compiler will first compile Main.java, and when it sees the reference to Utensil (which precedes the reference to Dessert), it will look in Utensil.java for this class and find both Utensil and Dessert. When the compiler encounters Dessert.java on the command line, it will pull in that file too, causing it to encounter both definitions of Utensil and Dessert. 如果你足夠幸運,使用 `javac Main.java Dessert.java` 命令編譯程序時,編譯將失敗,編譯器將告訴你多重定義了 Utensil 和 Dessert。這是因為編譯器將首先編譯 `Main.java`,當它看到對 Utensil 的引用(在對 Dessert 的引用之前)時,它將在 `Utensil.java` 中查找這個類,并找到餐具和甜點。當編譯器在命令行上遇到 `Dessert.java` 時,(編譯器)也會載入該文件,導致(編譯器)同時遇到 Utensil 和 Dessert 的定義。 If you compile the program with the command javac Main.java or javac Main.java Utensil.java, it will behave as it did before you wrote the Dessert.java file, printing pancake. But if you compile the program with the command javac Dessert.java Main.java, it will print potpie. The behavior of the program is thus affected by the order in which the source files are passed to the compiler, which is clearly unacceptable. 如果你使用命令 `javac Main.java` 或 `javac Main.java Utensil.java` 編譯程序,它的行為將與編寫 `Dessert.java` 文件(打印 pancake)之前一樣。但是如果你使用命令 `javac Dessert.java Main.java` 編譯程序,它將打印 potpie。因此,程序的行為受到源文件傳遞給編譯器的順序的影響,這顯然是不可接受的。 Fixing the problem is as simple as splitting the top-level classes (Utensil and Dessert, in the case of our example) into separate source files. If you are tempted to put multiple top-level classes into a single source file, consider using static member classes (Item 24) as an alternative to splitting the classes into separate source files. If the classes are subservient to another class, making them into static member classes is generally the better alternative because it enhances readability and makes it possible to reduce the accessibility of the classes by declaring them private (Item 15). Here is how our example looks with static member classes: 修復這個問題非常簡單,只需將頂層類(在我們的示例中是 Utensil 和 Dessert)分割為單獨的源文件即可。如果你想將多個頂層類放到一個源文件中,請考慮使用靜態成員類([Item-24](/Chapter-4/Chapter-4-Item-24-Favor-static-member-classes-over-nonstatic.md))作為將類分割為單獨的源文件的替代方法。如果(多個頂層類)隸屬于另一個類,那么將它們轉換成靜態成員類通常是更好的選擇,因為它增強了可讀性,并通過聲明它們為私有([Item-15](/Chapter-4/Chapter-4-Item-15-Minimize-the-accessibility-of-classes-and-members.md)),降低了類的可訪問性。下面是我們的靜態成員類示例的樣子: ``` // Static member classes instead of multiple top-level classes public class Test { public static void main(String[] args) { System.out.println(Utensil.NAME + Dessert.NAME); } private static class Utensil { static final String NAME = "pan"; } private static class Dessert { static final String NAME = "cake"; } } ``` The lesson is clear: Never put multiple top-level classes or interfaces in a single source file. Following this rule guarantees that you can’t have multiple definitions for a single class at compile time. This in turn guarantees that the class files generated by compilation, and the behavior of the resulting program, are independent of the order in which the source files are passed to the compiler. 教訓很清楚:永遠不要將多個頂層類或接口放在一個源文件中。遵循此規則可以確保在編譯時單個類不會擁有多個定義。這反過來保證了編譯所生成的類文件,以及程序的行為,與源代碼文件傳遞給編譯器的順序無關。 --- **[Back to contents of the chapter(返回章節目錄)](/Chapter-4/Chapter-4-Introduction.md)** - **Previous Item(上一條目):[Item 24: Favor static member classes over nonstatic(靜態成員類優于非靜態成員類)](/Chapter-4/Chapter-4-Item-24-Favor-static-member-classes-over-nonstatic.md)** - **Next Item(下一條目):[Chapter 5 Introduction(章節介紹)](/Chapter-5/Chapter-5-Introduction.md)**
                  <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>

                              哎呀哎呀视频在线观看