作者: hellokerby 時間: 2013-9-19 16:54 標題: 新手 JAVA 問題
本帖最後由 hellokerby 於 2013-9-19 16:56 編輯
段code 係示範Array 既例子
- class Account {
- int a;
- int b;
- public void setData(int c, int d) {
- a = c;
- b = d;
- }
- public void showData() {
- System.out.println("Value of a =" + a);
- System.out.println("Value of b =" + b);
- }
- }
- class ObjectArray {
- public static void main(String args[]) {
- Account obj[] = new Account[2];
- obj[0] = new Account();
- obj[1] = new Account();
- obj[0].setData(1, 2);
- obj[1].setData(3, 4);
- System.out.println("For Array Element 0");
- obj[0].showData();
- System.out.println("For Array Element 1");
- obj[1].showData();
- }
- }
- public void setData(int c, int d) {
- a = c;
- b = d;
- }
作者: 死仔飽 時間: 2013-9-19 17:45
你係唔係想個 method 係 public void setData(int a, int b) ?
如果係既話你要咁寫
- public void setData(int a, int b) {
- this.a = a;
- this.b = b;
- }
作者: KinChungE 時間: 2013-9-19 18:01
- public void setData(int newA, int newB) {
- a = newA;
- b = newB;
- }
作者: alexyyc 時間: 2013-9-19 19:09
It is because you need to set parameters for the method setData().
So you may also change the code like this:
- public void setData(int number_1, int number_2) {
- a = number_1;
- b = number_2;
- }
