開発環境
- 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)の13章(オブジェクトのデストラクション)の13.8(練習問題)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 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 Horse;
our @ISA = qw(Animal);
sub sound { 'neigh' }
}
{ package RaceHorse;
our @ISA = qw(Horse);
dbmopen(our %STANDINGS, "standings", 0666) or die "can't dbmopen standings dbm: $!";
sub new {
my $self = shift->SUPER::new(@_);
my @standings = split ' ', $STANDINGS{$self->name} || "0 0 0 0";
@$self{qw(wins places shows losses)} = @standings;
$self;
}
sub DESTROY {
my $self = shift;
$STANDINGS{$self->name} = "@$self{qw(wins places shows losses)}";
$self->SUPER::DESTROY;
}
sub won { shift->{wins}++; }
sub placed { shift->{places}++; }
sub showed {shift->{shows}++;}
sub lost {shift->{losses}++;}
sub standings {
my $self = shift;
join ", ", map "$self->{$_} $_", qw(wins places shows losses);
}
}
my $runner = RaceHorse->new('Billy Boy');
$runner->won;
say $runner->name, ' has standings ', $runner->standings, '.';
入出力結果(Terminal)
$ ./sample.pl Billy Boy has standings 1 wins, 0 places, 0 shows, 0 losses. $ ./sample.pl Billy Boy has standings 2 wins, 0 places, 0 shows, 0 losses. $ ./sample.pl Billy Boy has standings 3 wins, 0 places, 0 shows, 0 losses. $ ./sample.pl Billy Boy has standings 4 wins, 0 places, 0 shows, 0 losses. $
ちなみにpython3.3の場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
## Copyright (C) 2013 by kamimura
#-*- coding: utf-8 -*-
import os
import pickle
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 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 Horse(Animal):
def getSound(self):
return 'neigh'
class RaceHorse(Horse):
def __init__(self, name):
Horse.__init__(self, name)
self._filename = 'standings'
if os.path.isfile(self._filename):
with open(self._filename, 'rb') as f:
self._standings = pickle.load(f)
else:
self._standings = dict.fromkeys(
['wins', 'places', 'shows', 'losses'], 0)
def won(self):
self._standings['wins'] += 1
def placed(self):
self._standings['places'] += 1
def showed(self):
self._standings['shows'] += 1
def lost(self):
self._standings['losses'] += 1
def getStandings(self):
with open(self._filename, 'wb') as f:
pickle.dump(self._standings, f)
return ', '.join(map(lambda x: '{0} {1}'.format(
self._standings[x], x),
['wins', 'places', 'shows', 'losses']))
runner = RaceHorse('Billy Boy')
runner.won()
print('{0} has standings {1}.'.format(
runner.getName(), runner.getStandings()))
入出力結果(Terminal)
$ ./sample.py Billy Boy has standings 1 wins, 0 places, 0 shows, 0 losses. $ ./sample.py Billy Boy has standings 2 wins, 0 places, 0 shows, 0 losses. $ ./sample.py Billy Boy has standings 3 wins, 0 places, 0 shows, 0 losses. $ ./sample.py Billy Boy has standings 4 wins, 0 places, 0 shows, 0 losses. $
0 コメント:
コメントを投稿