How do you return two values from a single method?

后端 未结 22 860
谎友^
谎友^ 2020-12-11 00:58

When your in a situation where you need to return two things in a single method, what is the best approach?

I understand the philosophy that a method should do one t

22条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-11 01:38

    My choice is #4. Define a reference parameter in your function. That pointer references to a Value Object.

    In PHP:

    class TwoValuesVO {
      public $expectedOne;
      public $expectedTwo;
    }
    
    /* parameter $_vo references to a TwoValuesVO instance */
    function twoValues( & $_vo ) {
      $vo->expectedOne = 1;
      $vo->expectedTwo = 2;
    }
    

    In Java:

    class TwoValuesVO {
      public int expectedOne;
      public int expectedTwo;
    }
    
    class TwoValuesTest {
      void twoValues( TwoValuesVO vo ) {
        vo.expectedOne = 1;
        vo.expectedTwo = 2;
      }
    }
    

提交回复
热议问题