C# 如何将方法的返回值打印到同一类的方法中
在MakeSound方法中,我必须打印GetSound方法返回的值。您可以从MakeSound调用GetSound()。这里是一个实现C# 如何将方法的返回值打印到同一类的方法中,c#,C#,在MakeSound方法中,我必须打印GetSound方法返回的值。您可以从MakeSound调用GetSound()。这里是一个实现 using System; public abstract class Astrodroid { public virtual string GetSound() { return "Beep beep"; } public void MakeSound() { //this method
using System;
public abstract class Astrodroid
{
public virtual string GetSound()
{
return "Beep beep";
}
public void MakeSound()
{
//this method should print the returned value of above function
}
}
您可以使用
this.GetSound()
引用同一对象中的方法如果使用MakeSound函数调用GetSound,您将面临什么实际问题?向我们展示如何使用Astrodroid。public void MakeSound(){Console.WriteLine(GetSound());}
class Program
{
public abstract class Astrodroid
{
public virtual string GetSound()
{
return "Beep beep";
}
public void MakeSound()
{
Console.WriteLine(this.GetSound());
Console.ReadLine();
}
}
public class MyClass:Astrodroid
{
}
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.MakeSound();
}
}