本帖最後由 buy 於 2010-10-23 16:57 編輯
class(類別) ; object(物件) ; instance(實例) 彼此的關係
EX: String s = new String("ABC")
S物件(object)是String類別(class)的一個實例(instance)- import java.io.*;//for 鍵盤輸入
- import java.lang.Math;
- import java.util.ArrayList;
- public class Hello
- {
- public static void main(String args[])throws IOException
- {
- Milk a = new Milk();
- Milk b = new Milk();
-
-
-
- a.Box = 1 ; a.Bottle = 3;
- b.Box = 2 ; b.Bottle = 5;
-
- a.add(b);
- a.get();
- }
- }
- class Milk{
- public int Box; //箱
- public int Bottle;//罐
-
- public void add(Milk b)
- {
- Box = Box + b.Box;
- Bottle = Bottle + b.Bottle;
- }
-
- public void get()
- {
- System.out.println("Box: "+Box + "Bottle: " + Bottle);
- }
- }
複製代碼 |