2012年12月12日水曜日

開発環境

『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-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;
    }
    public abstract void Speal();
    public abstract void Move();
    public override string ToString()
    {
        return "Name:" + name + " Weight:" + weight;
    }
    public bool Equals(Animal other)
    {
        if (this.weight == other.weight)
        {
            return true;
        }
        return false;
    }
    #region IComprable
    public int CompareTo(Animal obj)
    {
        return this.weight.CompareTo(obj.weight);
    }
    #endregion
}

class Cat : Animal
{
    public Cat(int weight, string name) :
        base(weight, name) { }
    public override void Speal()
    {
        Console.WriteLine("ニャーニャー");

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

class Dog : Animal
{
    public Dog(int weight, string name) :
        base(weight, name) { }
    public override void Speal()
    {
        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 pochi = new Dog(10, "Pochi");
        Dog taro = new Dog(20, "Taro");
        Animal[] animals = { tama, sora, pochi, taro };
        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
Name:Taro Weight:20
Name:Pochi Weight:10
Name:Sora Weight:15
Name:Tama Weight:5
動物のQueue
Name:Tama Weight:5
Name:Sora Weight:15
Name:Pochi Weight:10
Name:Taro Weight:20
続行するには何かキーを押してください . . .

ちなみにJavaScriptの場合。

コード(TextWrangler)

var Animal = function(weight, name){
  var weight = weight;
  var name = name;
  this.get_weight = function(){
    return weight;
  };
  this.get_name = function(){
    return name;
  };
};
Animal.prototype.speak = function(){
  throw "speakメソッドが定義されていません。";
};
Animal.prototype.move = function(){
  throw "moveメソッドが定義されていません。";
};
Animal.prototype.toString = function(){
  return "Name:" + this.get_name() + " Weight:" + this.get_weight();
};
var Cat = function(weight, name){
  Animal.apply(this, [weight, name]);
};
Cat.prototype = new Animal;
Cat.constructor = Cat;
Cat.prototype.speak = function(){
  $('#pre0').append("ニャーニャー\n");
};
Cat.prototype.move = function(){
  $('#pre0').append("するする\n");
};
var Dog = function(weight, name){
  Animal.apply(this, [weight, name]);
};
Dog.prototype = new Animal;
Dog.prototype.constructor = Dog;
Dog.prototype.speak = function(){
  $('#pre0').append("ワンワン\n");
};
Dog.prototype.move = function(){
  $('#pre0').append("てくてく");
};
var tama = new Cat(5, "Tama");
var sora = new Cat(15, "Sora");
var pochi = new Dog(10, "Pochi");
var taro = new Dog(20, "Taro");
var animals1 = [tama, sora, pochi, taro];
var animals2 = [tama, sora, pochi, taro];
$('#pre0').append("LIFO(Last in First Out)\n");
while(animals1.length > 0){
  $('#pre0').append( animals1.pop()+ "\n");
}
$('#pre0').append("FIFO(First in First Out)\n");
while(animals2.length > 0){
  $('#pre0').append( animals2.shift()+ "\n");
}


pythonの場合。

sample.py

コード(TextWrangler)

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

class Animal:
    def __init__(self, weight, name):
        self.weight = weight
        self.name = name
    
    def speak(self):
        raise Exception("speakメソッドが定義されていません。")
    
    def move(self):
        raise Exception("moveメソッドが定義されていません。")
    
    def __str__(self):
        return "Name:{0} Weight:{1}".format(self.name, self.weight)
    
    def __repr__(self):
        return "Name:{0} Weight:{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("てくてく")
        
if __name__ == '__main__':
    tama = Cat(5, "Tama")
    sora = Cat(15, "Sora")
    pochi = Dog(10, "Pochi")
    taro = Dog(20, "Taro")
    animals = [tama, sora, pochi, taro]
    print("LIFO(Last in First Out)")
    for animal in animals:
        print(animal)
    print("LILO(Last in Last Out)")
    while len(animals) > 0:
        print(animals.pop())

入出力結果(Terminal)

$ ./sample.py
LIFO(Last in First Out)
Name:Tama Weight:5
Name:Sora Weight:15
Name:Pochi Weight:10
Name:Taro Weight:20
LILO(Last in Last Out)
Name:Taro Weight:20
Name:Pochi Weight:10
Name:Sora Weight:15
Name:Tama Weight:5
$

0 コメント:

コメントを投稿