0

在我的表格顶部,我有:

public static int hoursInt;
public static int minutesInt;
public static int secondsInt;
public static int CompletedIn24;

numericUpDown然后再往下,在选择新跑步者时,我有以下内容将框重置为零:

private void lstRunners_SelectedIndexChanged(object sender, EventArgs e)
{

Runner selectedRunner = (Runner)lstRunners.SelectedItem;

numericUpDown1.Value = 0;
numericUpDown2.Value = 0;
numericUpDown3.Value = 0;
}

然后在Finish按钮单击事件中我有:

 hoursInt = Convert.ToInt32(numericUpDown1.Value);
 minutesInt = Convert.ToInt32(numericUpDown2.Value);
 secondsInt = Convert.ToInt32(numericUpDown3.Value);

if (lstRunners.SelectedIndex > -1 && hoursInt + minutesInt + secondsInt != 0)
        {
            // Obtain selected climber
            Runner selectedRunner = (Runner)lstRunners.SelectedItem;
            selectedRunner.Hours = hoursInt;
            selectedRunner.Minutes = minutesInt;
            selectedRunner.Seconds = secondsInt;

            var expertRunner = selectedRunner as Expert;
            if (expertRunner != null)
            {
                expertRunner.UponFinish();
            }

这是我的重写方法Expert : Runner

public override void UponFinish()
        {
            base.UponFinish();

            // The integer must increment by one if the time is 24:00:00 or less i.e. 23:59:59 would increment the integer as well
            if (Hours < 24 || (Hours == 24 && Minutes == 0 && Seconds == 0))
            {
                CompletedIn24++;
            }
        }

目前,UponFinish()方法 inRunner大括号内没有任何东西,因为我不确定是否需要任何东西?

我尝试将CompletedIn24整数输出到字符串,以查看单击按钮时它是否可以工作,但即使选择了专家赛跑者并且时间为 24:00:00 或更短,该值仍保持为零。整数没有增加,我不确定是什么导致了问题?

任何帮助,将不胜感激。

4

7 回答 7

1

只需as像这样使用关键字:

var runner = selectedRunner as Expert;
if(runner != null) runner.UponFinish();

如果您的类Runner已经定义了一些名为 的方法UponFinish,则应将此方法定义为virtual并在派生类中覆盖该方法,如下所示:

public class Runner {
  public virtual void UponFinish(){
     //...
  }
}
public class Expert : Runner {
  public override void UponFinish(){
    //You talked about the time, I asked for clarification on this
    //but it's still very unclear. I suppose when you mean the time is 24:00:00
    //that means the hours is 24, the minutes is 0 and the seconds is 0
    if(Hours < 24 || (Minutes == 0 && Seconds == 0)) Completedin24++;
  }
}

然后当然你不需要任何演员,只需调用UponFinish和覆盖的代码(如果有的话)将被正确调用:

selectedRunner.UponFinish(); 
于 2013-11-07T14:12:20.673 回答
0

您可以像这样检查类型:

if (selectedRunner.GetType() == typeof(Expert))
{
    Expert expert = (Expert)selectedRunner;
}
于 2013-11-07T14:11:07.507 回答
0

你可以做

if(selectedRunner is Expert)
{
    UponFinish((Expert)selectedRunner);
    //or ((Expert)selectedRunner).UponFinish(); if that was the intention
}

或者

Expert selectedExpert = selectedRunner as Expert;
if(selectedExpert != null)
    UponFinish(selectedExpert);

编辑:

如果您的UponFinish函数已经是两者的一部分Runner并且Expert(即在 中被覆盖Expert),则无需在调用它之前强制转换 selectedRunner。

于 2013-11-07T14:11:17.593 回答
0

有几种方法可以做到这一点,is例如存在运算符: 运算符 IS

于 2013-11-07T14:11:57.720 回答
0

您可以使用 is 关键字检查您的 Runner 是否为 ExpertRunner:

if(selectedRunner is ExpertRunner)

但是,就 OOP 而言,您永远不必这样做,您可能需要检查您的层次结构或逻辑,为什么您需要单独处理这种情况而不是覆盖行为(函数或属性)。

于 2013-11-07T14:12:00.133 回答
0

尝试这个 :

if(lstRunners.SelectedItem is Expert)
{
    Expert selectedRunner = lstRunners.SelectedItem as Expert;
    selectedRunner.UponFinish();
}
于 2013-11-07T14:12:04.707 回答
0
if (lstRunners.SelectedItem is Expert)
{
    ((Expert)lstRunners.SelectedItem).UponFinish();
}
于 2013-11-07T14:12:15.707 回答