作者s4300026 (s4300026)
看板C_Sharp
标题[问题] 有什麽 继承 的好办法?
时间Sat Mar 9 16:43:46 2019
各位大大好,个人想询问继承的问题,
假设我有个方法执行内容完全,只有在继承的时候需要多了一些判断
想询问该怎麽撰写会比较好?
下面是情境举例,
想问有人有好解法吗?
----- 举例 -----
我有个父类别 BackgroundWorker Call_DoWork 执行如下
bool stop = false;
protected void Call_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
bool complete = false;
while(!worker.CancellationPending & !stop & !complete)
{
Step1();
if(
!worker.CancellationPending & !stop)
Step2();
if(
!worker.CancellationPending & !stop)
Step3();
if(
!worker.CancellationPending & !stop)
{
Step4();
complete = true;
}
}
}
我有个子类别继承父类别并override Call_DoWork 如下
override protected void Call_DoWork(object sender, DoWorkEventArgs e)
{
var worker = sender as BackgroundWorker;
bool complete = false;
while(!worker.CancellationPending & !stop & !complete &
rs232.IsOpen)
{
Step1();
if(
!worker.CancellationPending & !stop &
rs232.IsOpen)
Step2();
if(
!worker.CancellationPending & !stop &
rs232.IsOpen)
Step3();
if(
!worker.CancellationPending & !stop &
rs232.IsOpen)
{
Step4();
complete = true;
}
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 60.250.235.221
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_Sharp/M.1552121029.A.214.html
※ 编辑: s4300026 (60.250.235.221), 03/09/2019 16:52:13
※ 编辑: s4300026 (60.250.235.221), 03/09/2019 17:10:56
1F:推 O187: 把判断的部分提出来作另一个方法 03/09 17:45
2F:→ O187: 再override该方法 03/09 17:45
3F:→ s4300026: 对耶 我怎麽没想到,我笨了 哈哈 03/09 17:46