2007-11-08

Re: オブジェクト指向におけるFizzBuzz問題

http://blogs.wankuma.com/episteme/archive/2007/11/08/106927.aspx

かなりテキトー。エラー処理とかしない。


package Animal;
sub new      { bless { cnt => 1 } , $_[0] }
sub Sound    { printf "%s\n", $_[0]->{voice} x $_[0]->{cnt}  }
sub SetCount { $_[0]->{cnt}   = $_[1]; $_[0] }
sub SetVoice { $_[0]->{voice} = $_[1]; $_[0] }

package Dog;
use base qw/Animal/;
sub new { $_[0]->SUPER::new->SetVoice('Wan') }

package Cat;
use base qw/Animal/;
sub new { $_[0]->SUPER::new->SetVoice('Nya-') }

my $animal;
$animal = Dog->new;
$animal->Sound;
$animal->SetCount(3);
$animal->Sound;
$animal = Cat->new;
$animal->Sound;

わん、にゃーが文字化けしたのでローマ字で。

記事への反応(ブックマークコメント)

ログイン ユーザー登録
ようこそ ゲスト さん