Perl
Perlで動的ポリモーフィズムをしてみる
Perlで動的ポリモーフィズムしてみた。
動的型付け言語なので簡単だが、Perlのオブジェクト指向は書き方が面白いのでやってみた。
use strict; use warnings; package Ship; sub new { my $this = {}; return bless($this, $_[0]); } sub move { my $this = shift; print "moving...\n"; } sub fire { my $this = shift; print "fire!\n"; } package SuperShip; our @ISA = ('Ship'); sub new { my $this = {}; return bless($this, $_[0]); } sub fire { my $this = shift; print "fire!fire!\n"; } package HyperShip; our @ISA = ('Ship'); sub new { my $this = {}; return bless($this, $_[0]); } sub fire { my $this = shift; print "fire!fire!fire!\n"; } package main; my $s = Ship->new; $s->move(); $s->fire(); $s = SuperShip->new; $s->move(); $s->fire(); $s = HyperShip->new; $s->move(); $s->fire(); 1;
[ 戻る ]