将此方法拆分为c#
我有这个方法:将此方法拆分为c#,c#,C#,我有这个方法: public void makecall(int TransmitPhoneLoc, int ReceivePhoneLoc) { clsPhone Tphone = (clsPhone) phoneArray[TransmitPhoneLoc]; clsPhone Rphone = (clsPhone)phoneArray[ReceivePhoneLoc]; Tphone.ringPhone(); Rphone.ringPhone();
public void makecall(int TransmitPhoneLoc, int ReceivePhoneLoc)
{
clsPhone Tphone = (clsPhone) phoneArray[TransmitPhoneLoc];
clsPhone Rphone = (clsPhone)phoneArray[ReceivePhoneLoc];
Tphone.ringPhone();
Rphone.ringPhone();
//Tphone.hello();
}
我正在尝试将其分为两种方法,以便可以独立使用Tphone
和Rphone
。这是我最新的尝试。。。糟糕透了,我知道,但我正在努力
public clsPhone incoming(int TransmitPhoneLoc)
{
clsPhone Tphone = (clsPhone)phoneArray[TransmitPhoneLoc];
return Tphone;
}
public clsPhone Outgoing(int ReceivePhoneLoc)
{
clsPhone Rphone = (clsPhone)phoneArray[ReceivePhoneLoc];
return Rphone;
}
public void makecall()
{
// Rphone and Tphone do something?
}
我将非常感谢您对我如何独立使用这两个对象的任何帮助
编辑
问题是我不确定如何从我创建的两个方法而不是原始方法中获取值,并以与原始方法相同的方式实现它们。所以第三个方法是调用第一个和第二个方法。我已经尝试过这样做,因为我还有其他方法,比如endcall
这是我的最终解决方案,谢谢大家的尝试,我知道要理解像我这样的新手想要达到的目标并不容易。谢谢迪尔维德给了我最终解决方案的灵感。我希望现在我想做的事情已经清楚了。我的ClsPhone中有一个方法来设置连接,这将在ClsExchange中激活。将有其他方法来结束调用等
public clsPhone incoming(int TransmitPhoneLoc)
{
clsPhone Tphone = (clsPhone)phoneArray[TransmitPhoneLoc];
return Tphone;
}
public clsPhone Outgoing(int ReceivePhoneLoc)
{
clsPhone Rphone = (clsPhone)phoneArray[ReceivePhoneLoc];
return Rphone;
}
public void makecall(int tpc, int opc)
{
Outgoing(tpc).ringPhone();
incoming(opc).ringPhone();
}
不是很确定你想做什么,但是关于这一点呢?这是正确的吗
public void MakeCall(int phoneLoc)
{
clsPhone phone = (clsPhone)phoneArray[phoneLoc];
phone.ringPhone();
}
用法:由于OP代码显示了阵列中接收和发送电话的索引值的先验知识,因此该方法可以如下使用
MakeCall(tpc);
//and/or
MakeCall(opc);
或者您可以这样做
public clsPhone GetPhone(int phoneLoc)
{
return (clsPhone)phoneArray[phoneLoc];
}
用法:
GetPhone(tpc).ringPhone();
//and/or
GetPhone(opc).ringPhone();
类级使用选项:
public class MyClass
{
clsPhone[] phoneArray;//set this however
int tpc = 1;
int opc = 2;
clsPhone Tphone { get { return GetPhone(tpc); } }
clsPhone Rphone { get { return GetPhone(opc); } }
private clsPhone GetPhone(int phoneLoc)
{
return (clsPhone)phoneArray[phoneLoc];
}
public void MakeCall()
{
Tphone.ringPhone();
Rphone.ringPhone();
}
}
希望这有意义 我将前两种方法改为(忽略您选择的非标准命名约定): 最后一种方法是:
public void makecall() {
// You'll need to have the values for rPhoneLoc / tPhoneLoc set somewhere
// or as parameters to this method
var rPhone = Outgoing(rPhoneLoc);
var tPhone = Incoming(tPhoneLoc);
rPhone.ringPhone(tPhone);
}
我知道您的
ringPhone
方法当前可能不接受要调用的目标的参数。考虑到您正试图使用这两种方法,此重构可能是有意义的。我想您想要的是:
public void makecall(clsPhone Tphone, clsPhone Rphone)
{
Tphone.ringPhone();
Rphone.ringPhone();
}
那么你会这么做吗
public static void main()
{
var phone1 = incoming(213123) // just sample values, idk what int TransmitPhoneLoc is
var phone2 = Outgoing(345435)
makecall(phone1, phone2)
}
为什么不进一步简化它:
public MakeCall(int phoneLocation)
{
clsPhone phone = (clsPhone)phoneArray[phoneLocation];
phone.ringPhone();
}
...
MakeCall(transmitPhoneLoc);
MakeCall(receivePhoneLoc);
我相信你在追求
public clsPhone Tphone(int TransmitPhoneLoc)
{
clsPhone tempPhone = (clsPhone)phoneArray[TransmitPhoneLoc];
return tempPhone
}
public clsPhone Rphone(int ReceivePhoneLoc)
{
clsPhone tempPhone = (clsPhone)phoneArray[ReceivePhoneLoc];
return tempPhone
}
public void makecall()
{
//use
Tphone(1234).ringPhone();
//or
Rphone(1234).ringPhone();
}
我想我不明白这个问题。编辑它,让我们了解这些对象是如何相互作用的。制造商需要给接受者打电话吗?或者给我们一些业务规则或想法,说明这一切应该做些什么。顺便说一下,您的代码很难阅读,因为您使用的是与标准惯例相反的大写类型和变量。类、类型和方法应该大写,局部变量应该小写。除了Moozhe所说的,您使用的是匈牙利符号,缩写不常见,使用的是后期绑定集合。。。我可以继续说下去,但总的来说,呃,我甚至很难找到问题所在。你的最终解决方案不是很好的练习。您不应该有两个函数来执行相同的任务。请参阅我帖子中的备选答案,了解如何使用一个功能来获取您想要拨打的电话,但谁的电话是
phone
ringing to?我想你会给它传递一个接收对象。@john:它会根据“phoneLoc”参数来响电话。op显然已经根据他们私下编写的两种方法知道了这些。但是你的答案在接收和发送之间没有进行正确的通信。@JonH:在op代码中,除了参数名称之外,接收和发送之间没有区别。两者都使用相同的类类型(clsPhone)和相同的数组(phoneArray),但这就是问题所在,这样一个类的目的是什么,这最终可能是OP所关心的问题,而您的答案则悬而未决。不投赞成票,也不投反对票,只是说说而已。不是100%,而是把答案写出来!!Thanx.不确定您是否希望有两种方法执行相同的操作there@DylanJackson:请不要使用两种相同的方法thing@Dilvid-读一读枯燥的原则…@James我知道这些原则,在过去的10多年里每天都在做。我只是调整用户代码使其正常工作,以便他理解答案。把事情复杂化是没有意义的,最好在OP post的级别上发言。
public clsPhone Tphone(int TransmitPhoneLoc)
{
clsPhone tempPhone = (clsPhone)phoneArray[TransmitPhoneLoc];
return tempPhone
}
public clsPhone Rphone(int ReceivePhoneLoc)
{
clsPhone tempPhone = (clsPhone)phoneArray[ReceivePhoneLoc];
return tempPhone
}
public void makecall()
{
//use
Tphone(1234).ringPhone();
//or
Rphone(1234).ringPhone();
}