问答详情
源自:3-1 静态成员

求帮忙看下哪儿错了

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; 
namespace PetShop
{
    struct fish
    {
        int weight;
        int size;
        int type;
    }
    interface ICatchMice    {
        void CatchMice();
    }
    interface IClimbTree    {
        void climbTree();
    }
    abstract public class Pet    {
        public Pet(string name)
        {
            _name = name;
        }
        protected string _name;
        public void PrintName()
        {
            Console.WriteLine("Pet's name is " + _name);
        }
        //public uint age;        //public string food;        abstract public void Speak();
        virtual public void Move()
        {
 
        }
       
    }
    public class Dog:Pet    {
        static int Num;
        static Dog()
        {
            Num = 0;
        }
        public Dog(string name):base(name)
        {
            ++Num;
        }
        new public void PrintName()
        {
            Console.WriteLine("宠物的名字是 " + _name);
        }
        sealed override public void Speak()
        {
            Console.WriteLine(_name + " is speaking" + " wow");
        }
        override public void Move()
        {
 
        }
        static public void ShowNum()
        {
            Console.WriteLine("Dog's number = " + Num);
        }
    }
    public class Labrador : Dog    {
        public Labrador(string name):base(name)
        {
 
        }
    }
    public class Cat:Pet,ICatchMice,IClimbTree    {
        public Cat(string name):base(name)
        {
     
        }
        override public void Speak()
        {
            Console.WriteLine(_name + " is speaking" + " meow");
        }
        public void CatchMice()
        {
            Console.WriteLine(" Catch mice");
        }
        public void ClimbTree()
        {
            Console.WriteLine(" Climb tree");
        }
    }
 
    class Program    {
        static void Main(string[] args)
        {
            Pet[] pets = new Pet[] {new Dog("Jack"),new Cat("Tom")};
            //Pet dog = new Dog();            //dog.PrintName();            //dog.Speak();            ////Pet cat = new Cat();            //cat.PrintName();            //cat.Speak();            for (int i = 0; i < pets.Length; ++i)
            {
                pets[i].Speak();
            }
            Cat c = new Cat("Tom2");
            IClimbTree climb = (IClimbTree)c;
            c.ClimbTree();
            climb.ClimbTree();
            ICatchMice catchM = (ICatchMice)c;
            c.CatchMice();
            catchM.CatchMice();
 
            Dog.ShowNum();
        }
    }
}

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

提问者:慕粉4367945 2018-10-26 21:42

个回答

  • qq_慕仔2384744
    2022-06-03 19:10:58

    的我

    https://s.cnr.cn/index_sub.html?key=%E4%B9%B0%E7%90%83%E5%B9%B3%E5%8F%B0%E3%80%90%E5%AE%98%E7%BD%91%EF%BC%9AY%D0%92%E2%91%A1%E2%91%A4%E2%91%A1.%D0%A1%D0%A1%E3%80%91


  • weixin_慕少4105335
    2020-03-28 19:21:17

    第一点错误,派生类重写基类的方法,基类没有方法

     virtual  public void Speak()

            {

             }

    第二点错误,你声明接口,

    void climbTree();
    C是小写,你调用又是大写,所以提示错误。


  • qq_慕函数7518432
    2019-07-09 19:16:26

    好像类中写的ClimbTree()方法是小写的 调用的时候是大写的吧

  • Yoonlong
    2018-10-27 15:42:36

        interface IClimbTree

        {

            void ClimbTree();

        }

    大写啊ClimbTree();