開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語:Perl
その他参考書籍
1.
コード(BBEdit)
sample.pl
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
{
package Animal;
sub sound {die "動物のsoundを定義し忘れてる!";}
sub named {
my $class = shift;
my $name = shift;
my $self = {Name => $name, Color => $class->default_color};
bless $self, $class;
}
sub speak {
my $either = shift;
print $either->name, " goes ", $either->sound, "\n";
}
sub name {
my $either = shift;
ref $either ? $either->{Name} : "an unnamed $either";
}
sub default_color {'brown'}
sub color {
my $either = shift;
ref $either ? $either->{Color} : $either->default_color;
}
sub set_name {
my $self = shift;
$self->{Name} = shift;
}
sub set_color {
my $self = shift;
$self->{Color} = shift;
}
}
{
package Horse;
our @ISA = qw(Animal);
sub sound { "neigh" }
}
{
package RaceHorse;
our @ISA = qw(Horse);
dbmopen our %STANDINGS, "standings", 0666
or die "Can't open standings:$!\n";
sub DESTROY {
my $self = shift;
my $name = $self->name;
$STANDINGS{$name} = "@$self{qw(wins places shows losses)}";
$self->SUPER::DESTROY;
}
sub named {
my $self = shift->SUPER::named(@_);
my $name = $self->name;
my @standings = split ' ', $STANDINGS{$name} || "0 0 0 0";
@$self{qw(wins places shows losses)} = @standings;
$self;
}
sub won {
shift->{wins}++;
}
sub lost {
shift->{losses}++;
}
sub showed {
shift->{shows}++;
}
sub placed {
shift->{places}++;
}
sub standings{
my $self = shift;
join ", ", map "$self->{$_} $_", qw(wins places shows losses);
}
}
my $runner = RaceHorse->named('Billy Boy');
$runner->won;
print $runner->name, ' has standings ', $runner->standings, ".\n";
入出力結果(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. $
pythonの場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
import os, pickle
class Animal:
name = "Animal"
sound = "鳴き声"
color = "brown"
def __init__(self, name):
if name:
self._name = name
else:
self._name = Animal.name
self._sound = Animal.sound
self._color = Animal.color
def getName(self):
return self._name
def setName(self, name):
self._name = name
def getColor(self):
return self._color
def setColor(self, color):
self._color = color
def speak(self):
print("{0} goes {1}".format(self._name, self._sound))
@staticmethod
def staticSpeak():
print("{0} goes {1}".format(Animal.name, Animal.sound))
class Horse(Animal):
name = "Horse"
sound = "neigh"
class RaceHorse(Horse):
file_name = 'standings.db'
def __init__(self, name):
Animal.__init__(self, name)
if os.path.isfile(RaceHorse.file_name):
with open(RaceHorse.file_name, "rb") as f:
self._standings = pickle.load(f)
else:
self._standings = {"wins":0, "places":0, "shows":0, "losses":0}
def won(self):
self._standings["wins"] += 1
def lost(self):
self._standings["losses"] += 1
def shows(self):
self._standings["shows"] += 1
def placed(self):
self._standings["places"] += 1
def standings(self):
with open(RaceHorse.file_name, '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.standings()))
入出力結果(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 コメント:
コメントを投稿