2013年3月5日火曜日

開発環境

『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第14章(ジェネリックとコレクション)14.6(練習問題)練習14-3.を解いてみる。

その他参考書籍

練習14-3.

コード

using System;
using System.Collections.Generic;

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 name + ", " + weight;
    }
    public int CompareTo(Animal other)
    {
        return this.weight.CompareTo(other.weight);
    }
}
class Dog : Animal
{
    public Dog(int weight, string name) :
        base(weight, name) { }
    public override void Speak()
    {
        Console.WriteLine("ワンワン");
    }
    public override void Move()
    {
        Console.WriteLine("てくてく");
    }
}
class Cat : Animal
{
    public Cat(int weight, string name) :
        base(weight, name) { }
    public override void Speak()
    {
        Console.WriteLine("ニャーニャー");
    }
    public override void Move()
    {
        Console.WriteLine("するする");
    }
}
class Tester
{
    public void Run()
    {
        Cat tama = new Cat(5, "Tama");
        Cat sora = new Cat(15, "Sora");
        Dog frisky = new Dog(10, "Frisky");
        Dog laika = new Dog(20, "Laika");
        Animal[] animals = { tama, sora, frisky, laika };
        Stack<Animal> animalStack = new Stack<Animal>();
        Queue<Animal> animalQueue = new Queue<Animal>();
        foreach (Animal animal in animals)
        {
            animalStack.Push(animal);
            animalQueue.Enqueue(animal);
        }
        Console.WriteLine("Stack");
        foreach (Animal animal in animalStack)
        {
            Console.WriteLine(animal);
        }
        Console.WriteLine("Queue");
        foreach (Animal animal in animalQueue)
        {
            Console.WriteLine(animal);
        }
    }
    static void Main()
    {
        Tester t = new Tester();
        t.Run();
    }
}

入出力結果(Console Window)

Stack
Laika, 20
Frisky, 10
Sora, 15
Tama, 5
Queue
Tama, 5
Sora, 15
Frisky, 10
Laika, 20
続行するには何かキーを押してください . . .

ちなみにJavaScriptの場合。

コード(BBEdit)

var Animal = function (weight, name) {
    var weight = weight
        name = name;
    this.getWeight = function () {
        return weight;
    },
    this.getName = function (  ) {
        return name;
    };
},
    Cat = function (  ) {
        Animal.apply(this, arguments);
    },
    Dog = function (  ) {
        Animal.apply(this, arguments);
    },
    animals = [],
    animalStack = [],
    animalQueue = [],
    tama, sora, frisky, laika, i, max;
    Animal.prototype.speak = function () {
        $('#pre0').append( "抽象speak\n" );
    },
    Animal.prototype.move = function (  ) {
        $('#pre0').append( "抽象move\n" );
    },
    Animal.prototype.toString = function(  ) {
        return this.getName() +", " + this.getWeight();
    },
    Cat.prototype = new Animal(),
    Cat.prototype.speak = function (  ) {
        $('#pre0').append("ニャーニャー\n");
    },
    Cat.prototype.move = function (  ) {
        $('#pre0').append("するする\n");
    },
    Dog.prototype = new Animal(),
    Dog.prototype.speak = function (  ) {
        $('#pre0').append("ワンワン\n");
    },
    Dog.prototype.move = function (  ) {
        $('#pre0').append("てくてく\n");
    },
    tama = new Cat(5, "Tama"),
    sora = new Cat(15, "Sora"),
    frisky = new Dog(10, "Frisky"),
    laika = new Dog(20, "Laika"),
    animals = [tama, sora, frisky, laika];
for (i = 0, max = animals.length; i < max; i += 1) {
    animalStack.push(animals[i]);
    animalQueue.push(animals[i]);
}
$('#pre0').append("Stack(FILO(First in Last Out)), pushとpop)\n");
while (animalStack.length > 0) {
    $('#pre0').append(animalStack.pop() + "\n");
}
$('#pre0').append("Queue(FIFO(First in First Out)), pushとshift)\n");
while (animalQueue.length > 0) {
    $('#pre0').append(animalQueue.shift() + "\n");
}



pythonの場合。

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

class Animal:
    def __init__(self, weight, name):
        self._weight = weight
        self._name = name
    def getWeight(self):
        return self._weight
    def speak(self):
        raise Exception("Animal speak")
    def move(self):
        raise Exception("Animal move")
    def __str__(self):
        return "{0}, {1}".format(self._name, self._weight)

class Cat(Animal):
    def speak(self):
        print("ニャーニャー")
    def move(self):
        print("するする")

class Dog(Animal):
    def speak(self):
        print("ワンワン")
    def move(self):
        print("てくてく")

tama = Cat(5, "Tama")
sora = Cat(15, "Sora")
frisky = Dog(10, "Frisky")
laika = Dog(20, "Laika")
animals = [tama, sora, frisky, laika]
animalStack = []
animalQueue = []
for animal in animals:
    animalStack.append(animal)
    animalQueue.append(animal)
print("Stack")
while len(animalStack) > 0:
    print(animalStack.pop())
print("Queue")
while len(animalQueue) > 0:
    print(animalQueue.pop(0))

入出力結果(Terminal)

$ ./sample.py
Stack
Laika, 20
Frisky, 10
Sora, 15
Tama, 5
Queue
Tama, 5
Sora, 15
Frisky, 10
Laika, 20
$

0 コメント:

コメントを投稿