2018年11月28日 星期三

four design examples for the add function as class or instance methods

一個運算的方法到底要定義為類別方法物件方法,完全取決於設計者。
底下以簡單的整數加法運算為例,說明定義為類別方法或物件方法時,製作及使用上的差別。
一般是類別設計者先決定運算要如何使用,再決定如何製作。

四種設計關鍵在於:  類別方法或物件方法 vs 結果回傳或覆蓋第1數

(1) 類別方法,回傳結果

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public static WholeNumber add(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      return n;
    }

    // n3:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n3 = WholeNumber.add(n1, n2);
      System.out.printf("n3:%s = n1:%s + n2:%s\n", n3, n1, n2);
    }
  } 


(2) 類別方法,結果覆蓋第1數

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public static void add(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      n1.number = n.number;
    }

    // n1:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n1_copy = new WholeNumber(3);
      WholeNumber.add(n1, n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1, n1_copy, n2);
    }
  } 


(3) 物件方法,回傳結果

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public WholeNumber add(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      return n;
    }

    // n3:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n3 = n1.add(n2);
      System.out.printf("n3:%s = n1:%s + n2:%s\n", n3, n1, n2);
    }
 } 


(4) 物件方法,結果覆蓋第1數

 public class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public void add(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      this.number = n.number;
    }

    // n1:7 = n1:3 + n2:4
    public static void main(String args[])
    {
      WholeNumber n1 = new WholeNumber(3);
      WholeNumber n2 = new WholeNumber(4);
      WholeNumber n1_copy = new WholeNumber(3);
      n1.add(n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1, n1_copy, n2);
    }
 }

以上四種寫法也可以合併成一支如下類別,方便比較四種用法的不同:

 class WholeNumber
 {
    public int number;

    public WholeNumber(int n)
    {
      this.number = n;
    }
    
    public WholeNumber(WholeNumber n)
    {
      this.number = n.number;
    }

    public String toString()
    {
      return String.format("%d", number);
    }

    public static WholeNumber classAddReturn(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      return n;
    }

    public static void classAddOverwrite(WholeNumber n1, WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = n1.number + n2.number;
      n1.number = n.number;
    }

    public WholeNumber instanceAddReturn(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      return n;
    }

    public void instanceAddOverwrite(WholeNumber n2)
    {
      WholeNumber n = new WholeNumber(0);
      n.number = this.number + n2.number;
      this.number = n.number;
    }
/*
n3:7 = n1:3 + n2:4
n1:7 = n1:3 + n2:4
n4:7 = n1:3 + n2:4
n1:7 = n1:3 + n2:4
*/
    public static void main(String args[])
    {
      WholeNumber n1,n2,n3,n4,n1_copy;
      
      n1 = new WholeNumber(3);
      n2 = new WholeNumber(4);
      
      n3 = WholeNumber.classAddReturn(n1, n2);
      System.out.printf("n3:%s = n1:%s + n2:%s\n", n3, n1, n2);
      
      n1_copy = new WholeNumber(n1);
      WholeNumber.classAddOverwrite(n1_copy, n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1_copy, n1, n2);
      
      n4 = n1.instanceAddReturn(n2);
      System.out.printf("n4:%s = n1:%s + n2:%s\n", n4, n1, n2);

      n1_copy = new WholeNumber(n1);
      n1_copy.instanceAddOverwrite(n2);
      System.out.printf("n1:%s = n1:%s + n2:%s\n", n1_copy, n1, n2);
    }
 } 

沒有留言: