開発環境
- OS: macOS High Sierra - Apple
- IDE(統合開発環境): Visual Studio for Mac
- プログラミング言語: C#
初めてのC# 第2版 (Jesse Liberty (著)、Brian MacDonald (著)、日向 俊二 (翻訳)、オライリージャパン)の14章(ジェネリックとコレクション)、14.6(練習問題)、練習14-4.を取り組んでみる。
コード
using System; using System.Collections.Generic; namespace sample14_1 { abstract class Animal : IComparable<Animal> { private int weight; private string name; public Animal(int weight, string name) { this.weight = weight; this.name = name; } abstract public void Speak(); abstract public void Move(); public override string ToString() { return String.Format("Name: {0}, Weight: {1}", name, weight); } public int CompareTo(Animal rhs) { return this.weight.CompareTo(rhs.weight); } public int CompareTo(Animal rhs, Animal.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: return 0; } } public class AnimalComparer: IComparer<Animal> { public enum ComparisonType { Weight, Name } private Animal.AnimalComparer.ComparisonType whichComparison; public Animal.AnimalComparer.ComparisonType WhichComparison { get { return whichComparison; } set { whichComparison = value; } } public int Compare(Animal lhs, Animal rhs) { return lhs.CompareTo(rhs, WhichComparison); } } } class Cat : Animal { public Cat(int weight, string name) : base(weight, name) { } public override void Speak() { Console.WriteLine("にゃーにゃー"); } public override void Move() { Console.WriteLine("スルスル"); } public override string ToString() { return string.Format("[Cat] {0}", base.ToString()); } } class Dog : Animal { public Dog(int weight, string name) : base(weight, name) { } public override void Speak() { Console.WriteLine("わんわん"); } public override void Move() { Console.WriteLine("テクテク"); } public override string ToString() { return string.Format("[Dog] {0}", base.ToString()); } } class Program { static void Display(List<Animal> animals) { foreach (var animal in animals) { Console.WriteLine(animal); } } static void Main(string[] args) { Cat cat1 = new Cat(10, "cat1"); Cat cat2 = new Cat(30, "cat2"); Dog dog1 = new Dog(20, "dog1"); Dog dog2 = new Dog(40, "dog2"); Animal[] animals = { cat1, cat2, dog1, dog2 }; Animal.AnimalComparer c = new Animal.AnimalComparer(); List<Animal> animalList = new List<Animal>(); foreach (var animal in animals) { animalList.Add(animal); } Display(animalList); Console.WriteLine("Default"); animalList.Sort(); Display(animalList); Console.WriteLine("Name"); c.WhichComparison = Animal.AnimalComparer.ComparisonType.Name; animalList.Sort(c); Display(animalList); Console.WriteLine("Weight"); c.WhichComparison = Animal.AnimalComparer.ComparisonType.Weight; animalList.Sort(c); Display(animalList); } } }
入出力結果(Terminal)
[Cat] Name: cat1, Weight: 10 [Cat] Name: cat2, Weight: 30 [Dog] Name: dog1, Weight: 20 [Dog] Name: dog2, Weight: 40 Default [Cat] Name: cat1, Weight: 10 [Dog] Name: dog1, Weight: 20 [Cat] Name: cat2, Weight: 30 [Dog] Name: dog2, Weight: 40 Name [Cat] Name: cat1, Weight: 10 [Cat] Name: cat2, Weight: 30 [Dog] Name: dog1, Weight: 20 [Dog] Name: dog2, Weight: 40 Weight [Cat] Name: cat1, Weight: 10 [Dog] Name: dog1, Weight: 20 [Cat] Name: cat2, Weight: 30 [Dog] Name: dog2, Weight: 40 Press any key to continue...
0 コメント:
コメントを投稿