C# 在声明方法变量时是否有使用条件的方法?
也许我缺少正确的语言来描述这一点,但我试图使用一个与当前传递的BO不同的预先存在的方法 我想应该是这样的:C# 在声明方法变量时是否有使用条件的方法?,c#,methods,variable-declaration,C#,Methods,Variable Declaration,也许我缺少正确的语言来描述这一点,但我试图使用一个与当前传递的BO不同的预先存在的方法 我想应该是这样的: public override SetInsurance(BusinessObjects.Utilities.LandViewer Viewer_land || BusinessObjects.Utilities.SeaViewer Viewer_sea, DataTable patient, int InsurancePriority) { } 感谢您的帮助,因为这可能根本不存在 */
public override SetInsurance(BusinessObjects.Utilities.LandViewer Viewer_land || BusinessObjects.Utilities.SeaViewer Viewer_sea, DataTable patient, int InsurancePriority)
{
}
感谢您的帮助,因为这可能根本不存在
*//请注意,这些BO有95%的相似性,但组合它们在我们的代码库中不是一个选项。您应该获取95%相似的位(或者至少是您希望在这种方法和其他可以使用任何一种类型的方法中使用的位),并将它们放在一个接口中,例如
接口IViewer
。让LandViewer
和SeaViewer
实现该接口,并让方法采用该接口,例如:
interface IViewer {
string Name {get;set;}
}
class LandViewer: IViewer {
public string Name {get;set;}
public int SomeValue;
}
class SeaViewer: IViewer {
public string Name {get;set;}
public string SomeOtherValue;
}
public override SetInsurance(IViewer viewer, DataTable patient, int InsurancePriority) {
Console.WriteLine(viewer.Name); //.Name is accessible as it's part of the 95%
// .SomeValue and .SomeOtherValue are not accessible, because they're not part of the 95%
}
您应该将95%相似的位(或者至少是您希望在本方法和其他方法中使用的、可用于任何一种类型的位)放入一个接口中,例如
interface IViewer
。让LandViewer
和SeaViewer
实现该接口,并让方法采用该接口,例如:
interface IViewer {
string Name {get;set;}
}
class LandViewer: IViewer {
public string Name {get;set;}
public int SomeValue;
}
class SeaViewer: IViewer {
public string Name {get;set;}
public string SomeOtherValue;
}
public override SetInsurance(IViewer viewer, DataTable patient, int InsurancePriority) {
Console.WriteLine(viewer.Name); //.Name is accessible as it's part of the 95%
// .SomeValue and .SomeOtherValue are not accessible, because they're not part of the 95%
}
你应该仔细阅读泛型。或者继承,并为它们都提供一个公共基类。或者接口。是否可以定义一个两个业务对象都将实现的接口?您能否至少在那个程度上修改业务对象?
函数重载
会有所帮助。您应该阅读泛型。或者继承,并为它们都提供一个公共基类。或者接口。是否可以定义一个两个业务对象都将实现的接口?您是否可以至少在该程度上修改业务对象?函数重载可以有所帮助。