-
-
Save akanehara/7aa8be09a38ee7c5c8965d4d89b1df30 to your computer and use it in GitHub Desktop.
ミニマルなPerlのOOメモ (B extends A)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package A; | |
| use strict; | |
| use warnings; | |
| #use fields(); | |
| sub new { | |
| my $class = shift; | |
| my $self = { | |
| @_, | |
| }; | |
| bless $self, $class; | |
| $self; | |
| } | |
| sub get_attr { | |
| my $self = shift; | |
| my $k = shift; | |
| $self->{$k}; | |
| } | |
| sub set_attr { | |
| my $self = shift; | |
| my $k = shift; | |
| my $v = shift; | |
| $self->{$k} = $v; | |
| } | |
| sub i_am { | |
| my $self = shift; | |
| "I am A"; | |
| } | |
| 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package B; | |
| use strict; | |
| use warnings; | |
| #use fields(); | |
| use base qw( A ); | |
| sub new { | |
| my $class = shift; | |
| my $self = $class->SUPER::new(@_); | |
| bless $self, $class; | |
| $self; | |
| } | |
| sub i_am { | |
| my $self = shift; | |
| my $base = $self->SUPER::i_am; | |
| "I am B. base says, \"$base\""; | |
| } | |
| my $b = B->new(foo=>1, bar=>99); | |
| printf("%s\n", $b->get_attr('foo')); | |
| printf("%s\n", $b->get_attr('bar')); | |
| printf("%s\n", $b->i_am()); | |
| 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment