1 /** 2 @author zst 3 @version 1.0 4 */ 5 public class Person{ 6 private String name; 7 private int age; 8 9 public void work(){ 10 11 } 12 }
說明: 1. 類名稱首字母要大寫,若是要定義的類名稱由多個單詞組成,則每一個單詞的首字母都要大寫2. 若是是對外公開的方法須要用「public」關鍵字修飾在上面Person類中:1. name,age 稱爲類數據成員,或字段2. work () 稱爲類的成員函數,或方法
1 Person xiaoMing = new Perosn();
1 /** 2 @author zst 3 @version 1.0 4 */ 5 class A 6 { 7 int i = 10; 8 int j = 10; 9 public static int k = 20; 10 11 public void f() 12 { 13 System.out.println("我是A類對象的方法"); 14 } 15 } 16 public class ObjectMemoryAllocation 17 { 18 public static void main(String[] args) 19 { 20 A aa = new A(); 21 aa.f(); 22 23 } 24 }