Создать собственное семейство с именем «Моя ферма», реализовать индексатор - C#

Узнай цену своей работы

Формулировка задачи:

Для этой программы нужно создать собственное семейство с именем «Моя ферма», реализовать индексатор , в основной программе продемонстрировать работу со всеми животными фермы.
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication1
{
    public abstract class Animal
    {
        string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public Animal()
        { name = "Животное без имени"; }
        public Animal(string ima)
        { name = ima; }
    }
    public class Собака : Animal
    {
        public Собака(string ima) : base(ima) { }
        public void Лает()
        { Console.WriteLine("Собака гавкает (гав), а корова мычит (муу) "); }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Animal[] cob = new Animal[1];
            Собака s = new Собака("Первая собака");
            cob[0] = s;
            ArrayList semeystvo = new ArrayList();
            semeystvo.Add(s);
            semeystvo.Add(new Собака("Вторая собака"));
            semeystvo.Add(new Собака("Первая курица"));
            Console.WriteLine("Начальное семейство: ");
            foreach (Animal dd in semeystvo)
            { Console.WriteLine(dd.Name); }
            bool f; int x = 0, i;
            for (i = 0; i < semeystvo.Count; ++i)
            {
                f = ((Animal)semeystvo[i]).Name.StartsWith("Собака");
                if (f == true) { x = i; break; }
            }
            semeystvo.RemoveAt(x);
            Console.WriteLine();
            Console.WriteLine("После удаления собаки: ");
            foreach (Animal dd in semeystvo)
            { Console.WriteLine(dd.Name); }
            Animal[] c = new Animal[3];
            for (i = 0; i < 3; ++i)
            { c[i] = new Собака("Корова " + (i + 1)); }
            semeystvo.AddRange(c);
            Console.WriteLine();
            Console.WriteLine("После добаления коров: ");
            foreach (Animal dd in semeystvo)
            { Console.WriteLine(dd.Name); }
            Console.WriteLine(); s.Лает();
            Console.ReadLine();
        }
    }
}

Решение задачи: «Создать собственное семейство с именем «Моя ферма», реализовать индексатор»

textual
Листинг программы
public abstract class Animal
{
    string name;
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public Animal()
    { name = "Животное без имени"; }
    public Animal(string ima)
    { name = ima; }
 
    public abstract string Say();
}
public class Dog : Animal
{
    public Dog(string ima) : base(ima) { }
    public override string Say()
    {
        return "GAV!";
    }
}
public class Cow : Animal
{
    public Cow(string ima) : base(ima) { }
    public override string Say()
    {
        return "Moo";
    }
}
 
public class Farm : IList<Animal>
{
    private Animal[] barn = new Animal[4];
 
    private void ExpandBarn()
    {
        Array.Resize(ref barn, barn.Length * 2);
    }
 
    #region IList<Animal> Members
 
    public int IndexOf(Animal item)
    {
        return Array.IndexOf(barn, item);
    }
 
    public void Insert(int index, Animal item)
    {
        if (Count + 1 == barn.Length)
            ExpandBarn();
        for (int i = Count; i > index; i--)
            barn[i] = barn[i - 1];
        barn[index] = item;
        ++Count;
    }
 
    public void RemoveAt(int index)
    {
        Array.Copy(barn, index + 1, barn, index, Count - index);
        --Count;
    }
 
    public Animal this[int index]
    {
        get
        {
            return barn[index];
        }
        set
        {
            barn[index] = value;
        }
    }
 
    #endregion
 
    #region ICollection<Animal> Members
 
    public void Add(Animal item)
    {
        if (Count == barn.Length)
            ExpandBarn();
        barn[Count++] = item;
    }
 
    public void Clear()
    {
        Array.Clear(barn, 0, Count);
        Count = 0;
    }
 
    public bool Contains(Animal item)
    {
        return Array.IndexOf(barn, item) > -1;
    }
 
    public void CopyTo(Animal[] array, int arrayIndex)
    {
        Array.Copy(barn, 0, array, arrayIndex, Count);
    }
 
    public int Count
    {
        get;
        protected set;
    }
 
    public bool IsReadOnly
    {
        get { return false; }
    }
 
    public bool Remove(Animal item)
    {
        int index = Array.IndexOf(barn, item);
        if (index == -1) return false;
        RemoveAt(index);
        return true;
    }
 
    #endregion
 
    #region IEnumerable<Animal> Members
 
    public IEnumerator<Animal> GetEnumerator()
    {
        foreach (Animal animal in barn)
            yield return animal;
    }
 
    #endregion
 
    #region IEnumerable Members
 
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
 
    #endregion
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

10   голосов , оценка 4.3 из 5
Похожие ответы