问答详情
源自:2-5 抽象方法和抽象类

程序没报错,为什么运行没有结果?求大神指教

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace ConsoleApp8

{

    public class Pet

    {

        public Pet(string name, uint age, string food)

        {

          _name=name;

            _age=age;

            _food=food;

        }

        protected string _name;

        protected uint _age;

        protected  string _food;

        protected string Voice;

       virtual public void PrintName()

        {

            Console.WriteLine("Pet's name is" + _name);

            Console.WriteLine("Pet's age is" + _age);

            Console.WriteLine("Pet's favorit food is" + _food);

        }


        virtual public void PrintVoice()

        {

            Console.WriteLine("Pet's voice is spking" );

        }

    }


    public class Dog:Pet

    {

        public Dog(string name, uint age, string food) : base(name, age, food)

        {

        }

    

        override public void PrintVoice()

        {

            Console.WriteLine(_name + "的叫声是:" + "汪汪汪");

        }

    }

    public class Cat:Pet

    {

        public Cat(string name, uint age, string food) : base(name, age, food)

        {

        }

        override public void PrintName()

        {

            Console.WriteLine("宠物的名字是:" + _name);

            Console.WriteLine("宠物的年龄是" + _age);

            Console.WriteLine("宠物最喜欢的食物是" + _age);

        }

        override public void PrintVoice()

        {

            Console.WriteLine(Name + "的叫声是:" + "喵喵喵");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Pet[] pets = new Pet[] { new Dog("旺财",7,"肉骨头"), new Cat("阿福", 3, "小鱼干") };


            for(int i=0; i<pets.Length; i++)

            {

                pets[i].PrintName();


            }

            for (int y = 0; y < pets.Length; y++)

            {

                pets[y].PrintVoice();

            }


        }

    }

}


提问者:Mrs_Q 2019-08-01 21:19

个回答

  • qq_慕侠5280997
    2020-05-15 17:55:26

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp8
    {
        public class Pet
        {
            public Pet(string name, uint age, string food)
            {
                _name = name;
                _age = age;
                _food = food;
            }
            protected string _name;
            protected uint _age;
            protected string _food;
            protected string Voice;
            virtual public void PrintName()
            {
                Console.WriteLine("Pet's name is" + _name);
                Console.WriteLine("Pet's age is" + _age);
                Console.WriteLine("Pet's favorit food is" + _food);
            }

            virtual public void PrintVoice()
            {
                Console.WriteLine("Pet's voice is spking");
            }
        }

        public class Dog : Pet
        {
            public Dog(string name, uint age, string food) : base(name, age, food)
            {
            }

            override public void PrintVoice()
            {
                Console.WriteLine(_name + "的叫声是:" + "汪汪汪");
            }
        }
        public class Cat : Pet
        {
            public Cat(string name, uint age, string food) : base(name, age, food)
            {
            }
            override public void PrintName()
            {
                Console.WriteLine("宠物的名字是:" + _name);
                Console.WriteLine("宠物的年龄是" + _age);
                Console.WriteLine("宠物最喜欢的食物是" + _food);
            }
            override public void PrintVoice()
            {
                Console.WriteLine(_name + "的叫声是:" + "喵喵喵");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Pet[] pets = new Pet[] { new Dog("旺财", 7, "肉骨头"), new Cat("阿福", 3, "小鱼干") };

                for (int i = 0; i < pets.GetLength(0); i++)
                {
                    pets[i].PrintName();

                }
                for (int y = 0; y < pets.GetLength(0); y++)
                {
                    pets[y].PrintVoice();
                }

            }
        }
    }

    /*楼上说的没错,Name这个错误比明显,另外是基类里定义的食物哪里:

    Console.WriteLine("Pet's favorit food is" + _age);         _age换成__food,不过这表面上的东西检查完了,还有关键的是,获取数组长度哪里语法有误,改正完就ok了,更正如下:

     for(int i=0; i<pets.GetLength(0); i++)

                {                pets[i].PrintName();            }

     for (int y = 0; y < pets.GetLength(0); y++)

                {               pets[y].PrintVoice();            }



  • qq_慕慕8513755
    2019-08-02 11:08:01


    https://img.mukewang.com/5d43a8ae0001c03d05750351.jpg

    最下面的Console.WritLine(Name+"..............");

    Name 改成_name,

    你原来设的是_name,没有Name