为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

            }


        }

    }

}


正在回答

2 回答

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



0 回复 有任何疑惑可以回复我~


https://img1.sycdn.imooc.com//5d43a8ae0001c03d05750351.jpg

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

Name 改成_name,

你原来设的是_name,没有Name


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信