建立一個類別作為公共類別庫
建立一個物件作為 Library books = new Library();
物件是類別的記憶體表示。
最重要的物件名稱不應大寫。
如何在Java中建立物件? {待定}
Java 提供了五種建立物件的方法。
Using new Keyword Using clone() method Using newInstance() method of the Class class Using newInstance() method of the Constructor class Using Deserialization
代碼:
public class Library { public static void main(String[] args) { Library books = new Library(); //Object Creation //new allocates memory - Instance - Instantiation books.fiction(); //Method Calling Statement Library librarian = new Library(); librarian.lending_books(); } public void fiction() //Method Signature { //Method Definition / Body System.out.println("Book Name:Fiction books"); } public void lending_books() //Method Signature { //Method Definition / Body System.out.println("Librarian:Successfully lended the books"); } }
輸出:
以上是Java基本程式及擴充的詳細內容。更多資訊請關注PHP中文網其他相關文章!