如何在 Java 中宣告一個類?


以下是如何宣告一個類的語法。

class className {
   //Body of the class
}

你可以透過在class關鍵字後面寫出類的名稱,然後加上大括號來宣告一個類。在這些花括號中,你需要定義類的正文(內容),即欄位和方法。

要讓所有(類)都可以訪問該類,你需要將其設為公共(public)。

public class MyClass {
   //contents of the class (fields and methods)
}

更新時間: 2019 年 7 月 30 日

13K+ 檢視次數

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.