2015-03-19 29 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