我正在通过本网站上的C#教程,我正在坚持这个练习。我被困在5.1和我工作的C#程序员发现这个问题是个怪异的空间。这让我很难看出我的概念是否正确。他说空间问题永远不会导致实际编译问题,因此它可能是网站验证器中的一个错误。
无论如何,我在这里练习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);
}
}
我得到的错误是:
并非所有要求都得到满足。
您必须定义一个名为GetSound的方法,该方法返回一个字符串。
任何人都可以帮我弄清楚我做错了吗?
编辑: 这是最终的解决方案。为帮助我实现目标的撰稿人标记答案!
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);
}
}
答案 0 :(得分:3)
您将GetSound
定义为属性,而不是方法。
public override string GetSound() { return "Beep bob"; }
就是你想要的。