dotnet academy 练习 7.1 - 继承和抽象 类 与覆盖?

dotnetacademy Exercise 7.1 - Inheritence and Abstract Classes with Override?

我正在学习本网站上的 C# 教程,但在这个练习中遇到了困难。我卡在了 5.1 上,与我一起工作的一位 C# 程序员发现这个问题非常可怕 space。这让我很难看出我的概念是否正确。他说 space 问题永远不会导致现实中的编译问题,因此它可能是站点上验证器中的错误。

无论如何,我在这里进行练习 7.1:dotnetacademy exercise 7.1,但我似乎无法正确验证代码。步骤如下:

1. Create an abstract class named Astrodroid that provides a virtual 
   method called GetSound which returns a string. The default sound
   should be the words "Beep beep".
2. Implement a method called 'MakeSound' which writes the result of   the GetSound method to the Console followed by a new line.
3. Create a derived class named R2 that inherits from Astrodroid.
4. Override the GetSound method on the R2 class so that it returns "Beep bop".

这是我写的代码:

using System;

// Implement your classes here.
public abstract class Astrodroid
{
    public virtual string GetSound { get { return "Beep beep"; } }

    public void MakeSound() 
    { 
        Console.WriteLine(GetSound); 
    }

}

public class R2 : Astrodroid
{
    public override string GetSound { get { return "Beep bob"; } }
}

public class Program
{
    public static void Main()
    {   
        //var MakeSound = new R2();
        //Console.WriteLine(MakeSound.GetSound);
    }
}

我得到的错误是这样的:

Not all requirements have been met.

You must define a method named GetSound that returns a string.

谁能帮我找出我做错了什么?

谢谢!努力学习!

编辑: 这是最终的解决方案。为帮助我做到这一点的贡献者标记答案!

using System;

// Implement your classes here.
public abstract class Astrodroid
{
    public virtual string GetSound () { return "Beep beep"; }

    public void MakeSound() 
    { 
        Console.WriteLine(GetSound()); 
    }

}

public class R2 : Astrodroid
{
    public override string GetSound () { return "Beep bop"; }
}

public class Program
{
    public static void Main()
    {   
        //var MakeSound = new R2();
        //Console.WriteLine(MakeSound.GetSound);
    }
}

您将 GetSound 定义为 属性,而不是方法。

public override string GetSound() { return "Beep bob"; }就是你想要的。