2013年2月7日木曜日

開発環境

『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也田中 慎司吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の11章(オブジェクト指向), 11.11(練習問題)1.を解いてみる。

その他参考書籍

1.

コード(BBEdit)

sample.pl

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';

{
    package Animal;
    sub speak {
        my $class = shift;
        print "a $class goes ", $class->sound, "!\n";
    }
}
{
    package Cow;
    our @ISA = qw(Animal);
    sub sound { "moooo" }
}
{
    package Horse;
    our @ISA = qw(Animal);
    sub sound { "neigh" }
}
{
    package Sheep;
    our @ISA = qw(Animal);
    sub sound { "baaaah" }
}
{
    package Mouse;
    our @ISA = qw(Animal);
    sub sound { "squeak" }
    sub speak {
        my $class = shift;
        Animal::speak($class);
        print "[but you can barely hear it!]\n";
    }
}
my @backyard = ();
print "1つ以上の小動物(Cow/Horse/Sheep/Mouse/)を入力(空白で終了)\n";
while (<>) {
    chomp;
    last if /^\s*$/;
    unless (/^(Cow|Horse|Sheep|Mouse)$/) {
        print "選択肢から小動物を選んでください\n";
    }
    push @backyard, $_;
}
for (@backyard) {
    $_->speak;
}

入出力結果(Terminal)

$ ./sample.pl
1つ以上の小動物(Cow/Horse/Sheep/Mouse/)を入力(空白で終了)
Cow
Horse
Sheep
Mouse
Cow

a Cow goes moooo!
a Horse goes neigh!
a Sheep goes baaaah!
a Mouse goes squeak!
[but you can barely hear it!]
a Cow goes moooo!
$

ちなみにJavaScriptの場合。

コード(BBEdit)

$('#pre0').html('');
var Animal = function ( ) {};
Animal.prototype.o = "Animal";
Animal.prototype.sound = "";
Animal.prototype.speak = function() {
    $('#pre0').append("a " + this.o + " goes " + this.sound + "!\n");
};
var Cow = function() {},
    Horse = function () {},
    Sheep = function () {},
    Mouse = function () {};
Cow.prototype = new Animal();
Horse.prototype = new Animal();
Sheep.prototype = new Animal();
Mouse.prototype = new Animal();
Cow.prototype.o = "Cow";
Horse.prototype.o = "Horse";
Sheep.prototype.o = "Sheep";
Mouse.prototype.o = "Mouse";
Cow.prototype.sound= "moooo";
Horse.prototype.sound = "neigh";
Sheep.prototype.sound = "baaaah";
Mouse.prototype.sound = "squeak";
Mouse.prototype.speak = function () {
    Animal.prototype.speak.apply(this);
    $('#pre0').append("[but you can barely hear it!]\n");
};
var animals = {"Cow":Cow, "Horse":Horse, "Sheep": Sheep, "Mouse":Mouse};
var backyard = [];
while (true) {
    var animal = prompt("小動物(Cow/Horse/Sheep/Mouse)を入力(空白で終了)","");
    if (/^\s*$/.test(animal)) {
        break;
    }
    if (! /^(Cow|Horse|Sheep|Mouse)$/.test(animal)) {
        alert("選択肢から小動物を選んでください");
    } else {
        backyard.push(new animals[animal]());
    }
}
var i;
for (i = 0, max = backyard.length; i < max; i += 1) {
    backyard[i].speak();
}



pythonの場合。

コード(BBEdit)

sample.py

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

import datetime, Oogaboogoo.date

td = datetime.date.today()
day = td.weekday() + 1 if td.weekday() < 6 else 0
print("Today is {0}, {1} {2}, {3}".format(  
     Oogaboogoo.date.getDay(day), Oogaboogoo.date.getMonth(td.month - 1),
     td.day, td.year))

入出力結果(Terminal)

$ ./sample.py
1つ以上の小動物(Cow/Horse/Sheep/Mouse/)を入力(空白で終了)Cow
1つ以上の小動物(Cow/Horse/Sheep/Mouse/)を入力(空白で終了)Horse
1つ以上の小動物(Cow/Horse/Sheep/Mouse/)を入力(空白で終了)Sheep
1つ以上の小動物(Cow/Horse/Sheep/Mouse/)を入力(空白で終了)Mouse
1つ以上の小動物(Cow/Horse/Sheep/Mouse/)を入力(空白で終了)Cow
1つ以上の小動物(Cow/Horse/Sheep/Mouse/)を入力(空白で終了)Dog
選択肢から小動物を選んでください
1つ以上の小動物(Cow/Horse/Sheep/Mouse/)を入力(空白で終了)
a Cow goes, moooo!
a Horse goes, neigh!
a Sheep goes, baaaah!
a Mouse goes, squeak!
[but you can barely hear it!]
a Cow goes, moooo!
$

0 コメント:

コメントを投稿