using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication6
{
   public abstract class Animal
    {
       public int x;
       public int y;
       public int X
       {
           get
           {
               return x;
           }
           set
           {
               X = ((value >= 1) && (value <= 168)) ? value : 0;
           }
       }
       public int Y
       {
           get
           {
               return y;
           }
           set
           {
               Y = ((value >= 1) && (value <= 168)) ? value : 0;
           }
       }
       public Animal(int m, int n)
       {
           X = m;
           Y = n;
       }
       public abstract void Move();    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication6
{
    class Fish:Animal 
    {
        public Fish(int m, int n)
            :base (m,n)
        {
            X += 10;
            Y += 10;
        }
        public override void Move()
        {
            Console.WriteLine("Fish:\nX={0}\nY={1}",X,Y);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication6
{
    class Frog:Animal 
    {
         public Frog(int m, int n)
            :base (m,n)
        {
            X += 20;
            Y += 20;
        }
        public override void Move()
        {
            Console.WriteLine("Frog:\nX={0}\nY={1}",X,Y);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication6
{
    class Bird:Animal 
    {
         public Bird(int m, int n)
            :base (m,n)
        {
            X += 30;
            Y += 30;
        }
        public override void Move()
        {
            Console.WriteLine("Fish:\nX={0}\nY={1}",X,Y);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            Fish f = new Fish(14, 16);
            Frog g = new Frog(45, 36);
            Bird b = new Bird(22, 56);
            Animal[] a = new Animal[2];
            a[0] = f;
            a[1] = g;
            a[2] = b;
            a[0].Move();
            a[1].Move();
            a[2].Move();
           
        }
    }
}

解决方案 »

  1.   

    public int X
      {
          get
          {
              return x;
          }
          set
          {
              x = ((value >= 1) && (value <= 168)) ? value : 0;
          }
      }
      public int Y
      {
          get
          {
              return y;
          }
          set
          {
              y = ((value >= 1) && (value <= 168)) ? value : 0;
          }
      }
    用大写会导致调用自己,进而造成堆栈溢出。
    Animal[] a = new Animal[3];