開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Perl
『続・初めてのPerl 改訂版』(Randal L. Schwartz, brian d foy, Tom Phoenix 著、伊藤 直也、田中 慎司、吉川 英興 監訳、株式会社ロングテール/長尾 高弘 訳、オライリー・ジャパン、2006年、ISBN4-87311-305-9)の12章(データのあるオブジェクト)の12.15(練習問題)1を解いてみる。
その他参考書籍
1.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use 5.016;
use utf8;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
binmode STDERR, ':utf8';
{ package LivingCreature;
sub speak {
my $either = shift;
if (@_) {
say "a $either goes '@_'";
} else {
say "a $either goes ", $either->sound, "!";
}
}
}
{ package Person;
sub sound { 'hmmmmm' }
our @ISA = qw(LivingCreature);
}
{ package Animal;
use Carp qw(croak);
our @ISA = qw(LivingCreature);
sub new {
ref(my $class = shift) and croak "class name needed";
my $name = shift;
my $self = {Name => $name, Color => $class->default_color};
bless $self, $class;
}
sub default_color{ 'brown' }
sub set_name {
ref(my $self = shift) or croak "instance variable(name) needed";
$self->{Name} = shift;
}
sub name {
my $either = shift;
ref $either
? $either->{Name}
: "an unnamed $either";
}
sub set_color {
ref(my $self = shift) or croak "instance variable(color) needed";
$self->{Color} = shift;
}
sub color {
my $either = shift;
ref $either
? $either->{Color}
: $either->default_color;
}
sub sound {
croak '動物のsoundが定義されていない!';
}
sub speak {
my $either = shift;
if (@_) {
croak "ドリトル博士ではないので、動物と話はできません!" if @_;
} else {
$either->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' }
sub color{ 'white' }
}
{ package Mouse;
our @ISA = qw(Animal);
sub sound { 'squeak' }
sub speak {
my $class = shift;
$class->SUPER::speak;
say "[but you can barely hear it!]";
}
}
my $tv_horse = Horse->new('Mr. Ed');
$tv_horse->set_name('Mister Ed');
$tv_horse->set_color('grey');
say $tv_horse->name, ' is ', $tv_horse->color;
say Sheep->name, ' colored ', Sheep->color, ' goes ', Sheep->sound;
入出力結果(Terminal)
$ ./sample.pl Mister Ed is grey an unnamed Sheep colored white goes baaaah $
ちなみにpython3.3の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-
import sys
class LivingCreature:
def speak(self, words=None):
if words:
print("a {0} goes '{1}'".format(
self.__class__.__name__, words))
else:
print('a {0} goes {1}!'.format(
self.__class__.__name__, self.sound()))
class Person(LivingCreature):
def getSound(self):
return 'hmmmmm'
class Animal(LivingCreature):
name = 'an unnamed'
def __init__(self, name):
self._name = name
self._color = 'brown'
def setName(self, name):
self._name = name
def getName(self):
return self._name
def setColor(self, color):
self._color = color
def getColor(self):
return self._color
def getSound(self):
print('動物のsoundが定義されていない')
sys.exit()
def speak(self, words=None):
if words:
print('ドリトル博士ではないので、動物と話はできません!')
else:
print('a {0} goes {1}!'.format(
self.__class__.__name__, self.sound()))
class Cow(Animal):
def getSound(self):
return 'moooo'
class Horse(Animal):
def getSound(self):
return 'neigh'
class Sheep(Animal):
name = Animal.name + ' Sheep'
color = 'white'
sound = 'baaaah'
def getSound(self):
return 'baaaah'
class Mouse(Animal):
def getSound(self):
return 'squeak'
def speak(self):
Animal.speak(self)
print('[but you can barely hear it!]')
animals = {'Animal':Animal, 'Cow': Cow, 'Horse': Horse, 'Sheep': Sheep,
'Mouse': Mouse}
tv_horse = Horse('Mr. Ed')
tv_horse.setName('Mister ED')
tv_horse.setColor('grey')
print('{0} is {1}'.format(tv_horse.getName(), tv_horse.getColor()))
print('{0} colored {1} goes {2}'.format(
Sheep.name, Sheep.color, Sheep.sound))
入出力結果(Terminal)
$ ./sample.py Mister ED is grey an unnamed Sheep colored white goes baaaah $
0 コメント:
コメントを投稿