【发布时间】:2022-07-28 02:37:54
【问题描述】:
知识水平: 我是一名新的 C# 程序员,这是我 6 年来第一次编写面向对象的代码。
背景: 我的程序从 excel 文档中读取有关单个设备(电机)的信息,对数据执行一些操作/转换,将数据转换为不同的类类型,然后将新的类信息写入数据库。
我想扩展现有方法以允许用户传入不同类别的数据。目前它只处理作为自定义 excel 类传入的信息。 我想扩展它以允许用户传入另一个类类型,具有不同但相似的参数。
我的问题的核心概念是关于编写可以处理多个不同但相似的类的方法的最佳实践。
我想了几种方法来修改我现有的方法,以便能够在两种不同但相似的类类型上执行。
- 两种方法。每个班级一个。每个类中的字段相似,但名称不同。这是这个问题的根本挑战。
- 相同的方法,但有多个 If - 似乎是一种糟糕的方法
- 将有问题的 Excel 类和新类合并到一个公共类中。让该方法仅在公共类上运行
其他澄清信息:
如果您想知道为什么这些类在存储的内容方面相似,但在字段名称方面却不同
*Excel 类从 excel 中读取设备信息。
*我试图支持的新类是通过从数据库中读取来填充的。该类具有数据库使用的所有相同字段(名称和数量)。
*我需要对这两个自定义类执行操作,以将存储在这些类中的信息转换为第三个自定义类,该类基于与第二类不同的单独数据库。
守则: 由于 foreach 处理,我不知道如何做我想要完成的事情。我需要同时遍历这两种类型。当前设置为仅适用于 DistQuery_Excel。 IDK 如何在此处包含其他类...
var DistQuery_Excel =
from motor in motor_Excel
orderby motor.MotorName //alpha order by motor name
group motor by motor.MotorDist; //string
var DistQuery_TEI =
from motor in motor_TEI
orderby motor.USER_TAG //alpha order by motor name
group motor by motor.Distribution_Equipment; //string
foreach (var distGroup in DistQuery_Excel) //for each Distribution Equipment group
{
int cirInc = 1; //if you need to make more circuits
//query circuits_DM for total num of circuits for that dist
DM_DistEquip dist = distLookupObj(distEquip_DM, distGroup.Key); //Get distEquip Obj from List of DM dist equip by looking up Pnl Name, return DM_DistEquip obj.
int dist_Num = dist.ixPanel; //get primary key for the panel
int numOfDevices = distGroup.Count(); //get number of equips for this dist
List<DMCircuit> circuits_Per_Dist = circuits_Avail.Where(cir => cir.ixPanel == dist_Num).OrderBy(cir => cir.iCirNum).ToList(); //Get available circuits for this Dist Equip. Sort numerically
foreach (var motor in distGroup)
{
DMMotor motorEx = ex_Load(motor.getName()); //check if motor exists in DM equipList_DM. Returns DM_Motor if found.
//EXISTING MOTOR
if (motorEx is not null) //if not null, is existing motor in DM. Update load information (HP, KVA, Load Factor)
{
motorEx.LT_HP_1 = Convert.ToString(motor.MotorHP); //HP
motorEx.LT_CON_1 = motor.MotorKVA; //KVA
//TODO: update LT_FAC_1 if load type has changed
//TODO: update Voltage/Phase
if (circuits_Per_Dist.Any()) //if circuits still available
{
motorEx.ixCircuit1 = circuits_Per_Dist[0].ixCircuit; //assign to next circuit available via circuit Primary ID.
UpdateDM_Circuit(motorEx.EQU_CALLOUT, distGroup.Key + "-" + circuits_Per_Dist[0].iCirNum, motorEx.LT_CON_1, circuits_Per_Dist[0].ixCircuit, DMpath); //Update Circuits Available. Will push this back out to TblCircuit
circuits_Per_Dist.RemoveAt(0); //remove the circuit from availble list
}//end of if
else //No circuits left, must add them
{
motorEx.ixCircuit1 = InsertDM_Circuit(motorEx.EQU_CALLOUT, distGroup.Key + "-" + (dist.iTaps + cirInc), motorEx.LT_CON_1, dist_Num, dist.iTaps + cirInc, DMpath); //create blank entries within tblCircuit
cirInc++;
}//end of else
equipList_EX.Add(motorEx); //Add motor to list. Will combine with equipList_NEW. Will push back out to tblEquL
}//end of if
//NEW MOTOR
else //is new load
{
DMMotor motorNew = new DMMotor().Excel2DM(motor); //convert excel motor data to DM, assign DM motor properties
if (circuits_Per_Dist.Any())
{
motorNew.ixCircuit1 = circuits_Per_Dist[0].ixCircuit; //assign to next circuit available via circuit Primary ID.
UpdateDM_Circuit(motorNew.EQU_CALLOUT, distGroup.Key + "-" + circuits_Per_Dist[0].iCirNum, motorNew.LT_CON_1, circuits_Per_Dist[0].ixCircuit, DMpath);
circuits_Per_Dist.RemoveAt(0); //remove the circuit from availble list
}//end of if
else
{
motorNew.ixCircuit1 = InsertDM_Circuit(motorNew.EQU_CALLOUT, distGroup.Key + "-" + (dist.iTaps + cirInc), motorNew.LT_CON_1, dist_Num, dist.iTaps + cirInc, DMpath); //create blank entries within tblCircuit
cirInc++;
}//end of else
equipList_NEW.Add(motorNew); //add motor to the list. Will combine with equipList_EX. Will push back out to tblEquL
}//end of else
}//end of foreach
}//end of foreach
【问题讨论】:
-
听起来您应该使用
interface。让两个类都实现一个公共interface(例如IMyInterface),然后将一个公共方法的参数定义为一个IMyInterface。 -
@JonathanWillcock 您能否提供有关“将常用方法的参数定义为 IMyInterface”的更多信息。您是否建议该接口可以具有 getName 方法?我看不出这将如何解决有关上述 ForEach 循环问题的问题。我仍然有两种不同的实体类型,所以同时处理这两种类型仍然是不可能的。
标签: c# class oop inheritance