2013年2月8日金曜日

開発環境

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

その他参考書籍

2.

コード(BBEdit)

sample.pl

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

{
    package LivingCreature;
    sub speak {
        my $class = shift;
        if (@_) {
            print "a $class goes @_\n";
        } else {
            print "a $class goes ", $class->sound, "\n";
        }
    }
}
{
    package Person;
    our @ISA = qw(LivingCreature);
    sub sound { 'hummmm' }
}
{
    package Animal;
    our @ISA = qw(LivingCreature);
    sub sound {die "動物のsoundを定義し忘れてる!";}
    sub speak {
        my $class = shift;
        die "ドリトル博士ではないので動物と話すことはできない。" if @_;
        $class->SUPER::speak;
    }
}
{
    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";
    }
}

Person->speak;
Person->speak("Hello, World!");

入出力結果(Terminal)

$ ./sample.pl
a Person goes hummmm
a Person goes Hello, World!
$

ちなみにJavaScriptの場合。

コード(BBEdit)

$('#pre0').html('');
var LivingCreature = function() {},
    Person = function () {},
    Animal = function () {},
    Cow = function () {},
    Horse = function () {},
    Sheep = function () {},
    Mouse = function () {},
    living_creatures = {"Person":Person, 
        "Cow":Cow, "Horse":Horse, "Sheep": Sheep, "Mouse":Mouse},
    $select = $('#living_creature'),
    result = "",
    said = $('#said').val(),
    living_creature;
LivingCreature.prototype.o = "LivingCreature";
LivingCreature.prototype.speak = function (a) {
    if (a) {
        $('#pre0').append("a " + this.o + " goes " + a + "\n");
    } else {
        $('#pre0').append("a " + this.o + " goes " + this.sound + "!\n");
    }
};
Person.prototype = new LivingCreature();
Person.prototype.o = "Person";
Person.prototype.sound = "hummmm";
Animal.prototype = new LivingCreature();
Animal.prototype.o = "Animal";
Animal.prototype.speak = function () {
    if (arguments.length > 0 ) {
        throw {
            type: "エラー",
            message: "ドリトル博士ではないので動物と話すことは出来ない"
        };
    }
    LivingCreature.prototype.speak.apply(this);
};
Animal.prototype.constructor = Animal;
Cow.prototype = new Animal();
Cow.prototype.o = "Cow";
Cow.prototype.sound = "moooo";
Horse.prototype = new Animal();
Horse.prototype.o = "Horse";
Horse.prototype.sound = "neigh";
Sheep.prototype = new Animal();
Sheep.prototype.o = "Sheep";
Sheep.prototype.sound = "baaaah";
Mouse.prototype = new Animal();
Mouse.prototype.o = "Mouse";
Mouse.prototype.sound = "squeak";
Mouse.prototype.speak = function () {
    Animal.prototype.speak.apply(this, arguments);
    $('#pre0').append("[but you can barely hear it!]\n");
};
try{
    living_creature = $('option:selected', $select).text();
    living_creature = new living_creatures[living_creature]();
    if (said) {
        living_creature.speak(said);
    } else {
        living_creature.speak();
    }
} catch(e) {
    $('#pre0').text(e.type + ": " + e.message);
}


 



pythonの場合。

コード(BBEdit)

sample.py

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

class LivingCreature:
    def speak(self, a=""):
        if a:
            print("a {0} goes {1}".format(self.name, a))
        else:
            print("a {0} goes {1}!".format(self.name, self.sound))
class Person(LivingCreature):
    name = "Person"
    sound = "hummmm"
class Animal(LivingCreature):
    name = "Animal"
    sound = "鳴き声"
    def speak(self, a=""):
        if a != "":
            raise Exception("ドリトル博士ではないので動物と離す事は出来ません")
        LivingCreature.speak(self)

class Cow(Animal):
    name = "Cow"
    sound = "moooo"
class Horse(Animal):
    name = "Horse"
    sound = "neigh"
class Sheep(Animal):
    name = "Sheep"
    sound = "baaaah"
class Mouse(Animal):
    name = "Mouse"
    sound = "squeak"
    def speak(self):
        Animal.speak(self)
        print("[but you can barely hear it!]")

p = Person()
p.speak()
p.speak("Hello, World!")

入出力結果(Terminal)

$ ./sample.py
a Person goes hummmm!
a Person goes Hello, World!
$

0 コメント:

コメントを投稿