[技術討論] 新手 JAVA 問題

本帖最後由 hellokerby 於 2013-9-19 16:56 編輯

段code 係示範Array 既例子
  1. class Account {
  2.         int a;
  3.         int b;

  4.         public void setData(int c, int d) {
  5.                 a = c;
  6.                 b = d;
  7.         }

  8.         public void showData() {
  9.                 System.out.println("Value of a =" + a);
  10.                 System.out.println("Value of b =" + b);
  11.         }
  12. }

  13. class ObjectArray {
  14.         public static void main(String args[]) {
  15.                 Account obj[] = new Account[2];
  16.                 obj[0] = new Account();
  17.                 obj[1] = new Account();
  18.                 obj[0].setData(1, 2);
  19.                 obj[1].setData(3, 4);
  20.                 System.out.println("For Array Element 0");
  21.                 obj[0].showData();
  22.                 System.out.println("For Array Element 1");
  23.                 obj[1].showData();

  24.         }
  25. }
複製代碼
唔明點解要int c, d,  乜無得直接用番a,b 咩 ?
  1. public void setData(int c, int d) {
  2.                 a = c;
  3.                 b = d;
  4.         }
複製代碼

你係唔係想個 method 係 public void setData(int a, int b) ?

如果係既話你要咁寫
  1.     public void setData(int a, int b) {
  2.                     this.a = a;
  3.                     this.b = b;
  4. }
複製代碼

TOP

  1. public void setData(int newA, int newB) {
  2.                 a = newA;
  3.                 b = newB;
  4.         }
複製代碼
咁明未?

TOP

It is because you need to set parameters for the method setData().
So you may also change the code like this:
  1. public void setData(int number_1, int number_2) {
  2.       a = number_1;   
  3.       b = number_2;
  4. }
複製代碼
number_1 and number_2 are just the integers that being passed into the method

TOP