简体   繁体   中英

How can i print the returned value of a method into the method of the same class

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
    }

}

In MakeSound method i have to print the value that is returned by the GetSound method.

You can call GetSound() from MakeSound. Here is an implementation.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM