2015-03-19 30 views
0

當談到Java時,我仍然是一個相對的新手,主要來自C#背景。我應該使用匿名內部類來模擬Java中的'out'參數嗎?

我討論缺乏Java方法與同事,以及如何解決這個「出」參數。他建議創建一個結構/類來保存各種參數並將其傳回。

有時,這種感覺「錯誤」我 - 特別是如果我有,我想用從一個更大的類返回的參數的子集的特殊方法。

所以我想知道如何使用匿名內聯類來實現這一點。下面的代碼示例。

這是一個明智的做法嗎?只是想知道這種感知的智慧是什麼。

public class MyClass { 

    Patient myPatient = null; 

    // An interface to enable us to return these variables in a single call 
    public interface VitalStatsResponse { public void returnStats(int bloodPressure, int heartRate); } 

    public class Patient { 

     int bloodPressure = 100; 
     int heartRate = 280; 
     // Lots of other variables here 

     public void calculateVitalStats(VitalStatsResponse response) 
     { 
      response.returnStats((bloodPressure * 2), (heartRate/10) ; 
     } 
    } 


    public void doWork() 
    { 
     // We want the patient's blood pressure and heart rate returned by a single method call, so use an anonymous inline class 
     myPatient.calculateVitalStats(new VitalStatsResponse() { 
      @Override 
      public void returnStats(int bloodPressure, int heartRate) { 
       // Handle returned variables here 
      } 
     }); 
    } 
} 
+0

就個人而言,我會在C#中使用「類來保存我關心的屬性」,而不是使用'out'參數 - 如果某組屬性具有有用的含義,爲什麼沒有類型呢?通常我發現做到這一點,「更大的階級」也可以使用它,並且變得更簡單。例如,Person類不應該有「DateOfBirth,Street1,Street2,Street3,ZipCode,Country」......他們應該(可能)有「DateOfBirth,Address」。 out參數方法對於* private *方法有一定意義,但我不是公衆的粉絲。 – 2015-03-19 12:05:43

回答

0

我也將使用「類來保存參數」過「聯匿名內部類」

public class MyClass implements VitalStatsResponse{ 

Patient myPatient = null; 
private ArrayList<VitalStatsResponse> response; 


void MyClass(ArrayList<VitalStatsResponse> response) { 

    this.response = response; 
} 


public class Patient { 

    int bloodPressure = 100; 
    int heartRate = 280; 
    // Lots of other variables here 

    public void calculateVitalStats() 
    { 
    for(int i = 0; i < response.length; i++) { 
      // call returnStats method of every registered callback 
      response.get(i).returnStats((bloodPressure * 2), (heartRate/10) ; 
     } 
    } 
} 

// any client can register/unregister callback via these methods 
void registerResponse(VitalStatsResponse response) { 
    this.response.add(response); 
} 

void unRegisterResponse(VitalStatsResponse response) { 
    this.response.remove(response); 
} 

public void doWork() 
{ 
    // We want the patient's blood pressure and heart rate returned by a single method call, so use an anonymous inline class 
    myPatient.calculateVitalStats(); 
} 

public void returnStats(int bloodPressure, int heartRate) { 

// implement the body according to this class requirement 
} 
} 
1

我會去創造一個VitalStats對象的簡單的解決方案。如果您需要患者的VitalStatus,那麼VitalStats是您的應用程序中的一個概念,可以表示爲對象。

public class VitalStatus { 

    final int bloodPressure; 
    final int heartRate; 

    public VitalStats(int bloodPressure, int heartRate) { 
     this.bloodPressure = bloodPressure; 
     this.heartRate = heartRate; 
    } 
} 

public class Patient { 

    int bloodPressure = 100; 
    int heartRate = 280; 
    // Other variables 

    public VitalStatus getVitalStatus() { 
     return new VitalStats(bloodPressured * 2, heartRate/2); 
    } 
} 

Out params是返回時間的程序解決方案。 Java主要適合面向對象的編程範例,因此不要害怕製作對象。如果你的班級做了很多複雜的事情,看看你是否可以把它分解成更小的更易於管理的部分,這適合於SOLID中的S.

+0

有趣。但它比我的建議更多的代碼!那麼爲什麼這更好? – 2015-03-20 08:44:06

+0

@CarlosP我更新了答案以包含更多信息。這不是更多的代碼,但代碼更容易讀取IMO。在OOP中,通常會有很多容易理解的小類。 – cyroxis 2015-03-20 11:52:33