開発環境
- 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;
binmode STDOUT, ':utf8';
binmode STDIN, ':utf8';
{
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 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;
$class->SUPER::speak;
print "[but you can barely hear it!]\n";
}
}
my $tv_horse = Horse->named('Mr. Ed');
$tv_horse->set_name('Mister Ed');
$tv_horse->set_color('grey');
print $tv_horse->name, " is ", $tv_horse->color, "\n";
print Sheep->name, ' colored ', Sheep->color, " goes ", Sheep->sound, "\n";
入出力結果(Terminal)
$ ./sample.pl Mister Ed is grey an unnamed Sheep colored brown goes baaaah $
ちなみにJavaScriptの場合。
コード(BBEdit)
$('#pre0').html('');
var Animal = function (o) {
var n = o,
color = "brown",
sound = "鳴き声";
this.speak = function() {
$('#pre0').append(n + " goes " + sound + "\n");
},
this.setName = function( o ) {
n = o;
},
this.getName = function( ) {
return n;
},
this.setColor = function( o ) {
color = o;
},
this.getColor = function ( ) {
return color;
};
},
Cow = function () {
var sound = "moooo";
Animal.apply(this);
},
Horse = function () {
var sound = "neigh";
Animal.apply(this);
},
Sheep = function () {
var sound = "baaaah";
Animal.apply(this);
},
tv_horse;
Animal.o = "an unnamed Animal";
Animal.color = Cow.color = Horse.color = Sheep.color = "brown";
Animal.sound = "鳴き声";
Cow.prototype = new Animal();
Cow.o = "an unnamed Cow";
Cow.sound = "moooo";
Horse.prototype = new Animal();
Horse.o = "an unnamed Horse";
Horse.sound = "neigh";
Sheep.prototype = new Animal();
Sheep.o = "an unnamed Sheep";
Sheep.sound = "baaaah";
tv_horse = new Horse("Mr. Ed");
tv_horse.setName("Mister Ed");
tv_horse.setColor("grey");
$('#pre0').append(tv_horse.getName() + " is " + tv_horse.getColor() + "\n");
$('#pre0').append(Sheep.o + " colored " + Sheep.color + " goes " + Sheep.sound + "\n");
pythonの場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
class Animal:
name = "Animal"
sound = "鳴き声"
color = "brown"
def __init__(self, name):
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 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!]")
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 Sheep colored brown goes baaaah $
0 コメント:
コメントを投稿