Drkcore

06 04 2009 perl Moose Tweet

Moose::Roleでwithするクラス

モダンperl入門のp.28で

+------+      +--------+
| Bird | <--- | Winged |
+------+      +--------+
    | 
    |
+--------+
| Pigeon |
+--------+

とあるので、BirdにwithでロールをつけてPigeonがBirdを継承すればよかろうと思ったが、それだと'Winged' requires the method 'fly' to be implemented by 'Bird'とBirdクラスでflyが実装されてないよっていうエラーになる。

package Animal;
use Moose;

package Winged;
use Moose::Role;
requires 'fly';

package Bird;
use Moose;
extends 'Animal';
# with 'Winged'; ここだとエラー

package Pigeon;
use Moose;
extends 'Bird';
with 'Winged'; # ここに記述
sub fly { print "Pigeons fly in the day\n" }

package main;

Pigeon->new->fly;

つまり、個別にwithしないといけないのかな。

+------+ 
| Bird | --------------------------------------------------+
+------+                                                   |
    |\__________________________                           |
    |                          |                           |
+------+      +--------+    +------+      +--------+    +------+      +--------+
| Bird | <--- | Winged |    | Owl  | <--- | Winged |    | Hawk | <--- | Winged |
+------+      +--------+    +------+      +--------+    +------+      +--------+

こんな感じ?。それとも鳥は翼を持つからflyメソッドをBirdクラスに直接書いてしまっても良いのだろうか?(ペンギンとかはここでは無しの方向で)

一応上の図には載せてないのだけどサンプルでは Animal->Mammal->Batという(鳥ではないけど飛ぶことのできる)コウモリクラスがあることを想定している。

ProductName モダンPerl入門 (CodeZine BOOKS)
牧 大輔
翔泳社 / ¥ 2,940 ()
在庫あり。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021