2018年5月25日金曜日

開発環境

初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)日向 俊二 (翻訳)、オライリージャパン)の14章(ジェネリックとコレクション)、14.6(練習問題)、練習14-4.を取り組んでみる。

コード

using System;
using System.Collections.Generic;

class Program
{
    public static void Display(List<Animal> animals)
    {
        foreach (Animal animal in animals)
        {
            Console.WriteLine(animal);
        }
    }
    static void Main(string[] args)
    {
        Cat cat1 = new Cat(10, "cat1");
        Cat cat2 = new Cat(30, "cat2");
        Cat cat3 = new Cat(50, "cat3");
        Dog dog1 = new Dog(20, "dog1");
        Dog dog2 = new Dog(40, "dog2");
        Dog dog3 = new Dog(60, "dog3");
        List<Animal> animals  = 
            new List<Animal> { cat1, cat2, dog1, dog2, cat3, dog3 };
        Animal.AnimalComparer c = 
            new Animal.AnimalComparer();

        Console.WriteLine("List");
        Display(animals);
        Console.WriteLine("ソート");
        animals.Sort();
        Display(animals);
        Console.WriteLine("Name");
        c.WhichComparison = Animal.AnimalComparer.ComparisonType.Name;
        animals.Sort(c);
        Display(animals);
        Console.WriteLine("Weight");
        c.WhichComparison = Animal.AnimalComparer.ComparisonType.Weight;
        animals.Sort(c);
        Display(animals);
    }
}
public abstract class Animal:IComparable<Animal>
{
    private float weight;
    private string name;

    public Animal(float weight, string name)
    {
        this.weight = weight;
        this.name = name;
    }

    public abstract void Speak();
    public abstract void Move();
    public override string ToString()
    {
        return "Name: " + name + ", Weight: " + weight;
    }

    public int CompareTo(Animal other)
    {
        return this.weight.CompareTo(other.weight);
    }

    public int CompareTo(
        Animal rhs, AnimalComparer.ComparisonType which)
    {
        switch (which)
        {
            case AnimalComparer.ComparisonType.Weight:
                return this.weight.CompareTo(rhs.weight);
            case AnimalComparer.ComparisonType.Name:
                return this.name.CompareTo(rhs.name);
            default:
                break;
        }
        return 0;
    }

    public class AnimalComparer : IComparer<Animal>
    {
        public enum ComparisonType
        {
            Weight,
            Name,
        }
        private ComparisonType whichComparison;

        public int Compare(Animal x, Animal y)
        {
            return x.CompareTo(y, whichComparison);
        }

        public ComparisonType WhichComparison
        {
            get { return whichComparison; }
            set { whichComparison = value; }
        }
    }
}

public class Cat : Animal
{
    public Cat(float weight, string name) : 
        base(weight, name)
    {
    }

    public override void Move()
    {
        Console.WriteLine("Cat: Move");
    }

    public override void Speak()
    {
        Console.WriteLine("Cat Speak");
    }

    public override string ToString()
    {
        return "Cat: " + base.ToString();
    }
}

public class Dog : Animal
{
    public Dog(float weight, string name) :
        base(weight, name)
    {
    }

    public override void Move()
    {
        Console.WriteLine("Dog Move");
    }

    public override void Speak()
    {
        Console.WriteLine("Dog Speak");
    }

    public override string ToString()
    {
        return "Dog: " + base.ToString();
    }
}

入出力結果(コマンドプロンプト)

List
Cat: Name: cat1, Weight: 10
Cat: Name: cat2, Weight: 30
Dog: Name: dog1, Weight: 20
Dog: Name: dog2, Weight: 40
Cat: Name: cat3, Weight: 50
Dog: Name: dog3, Weight: 60
ソート
Cat: Name: cat1, Weight: 10
Dog: Name: dog1, Weight: 20
Cat: Name: cat2, Weight: 30
Dog: Name: dog2, Weight: 40
Cat: Name: cat3, Weight: 50
Dog: Name: dog3, Weight: 60
Name
Cat: Name: cat1, Weight: 10
Cat: Name: cat2, Weight: 30
Cat: Name: cat3, Weight: 50
Dog: Name: dog1, Weight: 20
Dog: Name: dog2, Weight: 40
Dog: Name: dog3, Weight: 60
Weight
Cat: Name: cat1, Weight: 10
Dog: Name: dog1, Weight: 20
Cat: Name: cat2, Weight: 30
Dog: Name: dog2, Weight: 40
Cat: Name: cat3, Weight: 50
Dog: Name: dog3, Weight: 60
続行するには何かキーを押してください . . .

0 コメント:

コメントを投稿

関連コンテンツ