# 創建類別
在與 Main.java
同層的資料夾中,創建一個 Card.java
來撰寫類別。
其類別中含有三個值域 Field
,代表該類別裡的變數。
Card.java | class Card{ |
| |
| String name; |
| int balance; |
| int bouns; |
| } |
# 物件初始化
使用建構式,可以使物件透過運用參數的方式對類別中的值域做自訂義。
# 定義建構式
Card.java | class Card{ |
| |
| String name; |
| int balance; |
| int bouns; |
| |
| Card(String name,int balance, int bouns){ |
| this.name = name; |
| this.balance = balance; |
| this.bouns = bouns; |
| } |
| } |
# 初始化物件
App.java | public class App{ |
| public static void main(String[] args){ |
| Card[] cards = { |
| new Card("Zrn",1200,1), |
| new Card("Yan",1000,1), |
| new Card("Hun",500,0), |
| new Card("Gyn",300,0), |
| new Card("Ton",300,0) |
| }; |
| for(Card card : cards){ |
| System.out.println(card.name+" "+card.balance+" "+card.bouns); |
| } |
| } |
| } |
執行結果為:
| Zrn 1200 1 |
| Yan 1000 1 |
| Hun 500 0 |
| Gyn 300 0 |
| Ton 300 0 |
# 操作物件
在類別中,可定義一個或多個方法對不同的物件做出運算。
其實,就是類別中的函式。
# 定義方法
Card.java | class Card{ |
| |
| String name; |
| int balance; |
| int bouns; |
| Card(String name,int balance, int bouns){ |
| this.name = name; |
| this.balance = balance; |
| this.bouns = bouns; |
| } |
| |
| void store(int money){ |
| if(money > 0){ |
| this.balance += money; |
| if(money >= 1000) |
| this.bouns++; |
| }else{ |
| System.out.println("沒有人存負數的啦!!"); |
| } |
| } |
| } |
# 運用方法
App.java | import java.io.PrintStream; |
| import java.util.Scanner; |
| public class App{ |
| private static PrintStream printf; |
| public static void main(String[] args){ |
| Card[] cards = { |
| new Card("Zrn",1200,1), |
| new Card("Yan",1000,1), |
| new Card("Hun",500,0), |
| new Card("Gyn",300,0), |
| new Card("Ton",300,0) |
| }; |
| Scanner in = new Scanner(System.in); |
| for(Card card : cards){ |
| System.out.printf("%s 現有的存款為: %d 元\n",card.name,card.balance); |
| System.out.printf("%s, 要存多少元?\n",card.name); |
| card.store(in.nextInt()); |
| System.out.printf("%s 現在的存款為: %d 元\n\n",card.name,card.balance); |
| } |
| } |
| } |
# 物件封裝
在宣告值域時,若將 private
放置於型態的前方,可使其物件被設置為私有。
Card.java | class Card{ |
| |
| private String name; |
| private int balance; |
| private int bouns; |
| } |
這樣以後,就沒有辦法直接性的的取得該變數。若直接性的在 App.java
運用。取得該值域則會報錯。
| for(Card card : cards){ |
| System.out.printf("%s 現有的存款為: %d 元\n",card.name,card.balance); |
| } |
報錯訊息
| Exception in thread "main" java.lang.Error: Unresolved compilation problems: |
| The field Card.name is not visible |
| The field Card.balance is not visible |
若此時,我仍想要取得該用戶的資料,則必須使用方法取值。
Card.java 定義方法 | class Card{ |
| private String name; |
| private int balance; |
| private int bouns; |
| Card(String name,int balance, int bouns){ |
| this.name = name; |
| this.balance = balance; |
| this.bouns = bouns; |
| } |
| String getname(){ |
| return name; |
| } |
| int getbalance(){ |
| return balance; |
| } |
| int getbouns(){ |
| return bouns; |
| } |
| } |
App.java 使用方法 | for(Card card : cards){ |
| System.out.printf("%s 現在的存款為: %d 元\n",card.getname(),card.getbalance()); |
| } |
| Zrn 現在的存款為: 1200 元 |
| Yan 現在的存款為: 1000 元 |
| Hun 現在的存款為: 500 元 |
| Gyn 現在的存款為: 300 元 |
| Ton 現在的存款為: 300 元 |