Last active
November 6, 2016 09:51
-
-
Save duck8823/407cb8504ae53cf3b7bca5323203aaf3 to your computer and use it in GitHub Desktop.
Perl6のコンストラクタと継承
This file contains 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 Animal; | |
sub new { | |
my $pkg = shift; | |
return bless {name => {@_}->{name}}, $pkg; | |
} | |
sub name { | |
my $self = shift; | |
return $self->{name}; | |
} | |
1; |
This file contains 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 Bird; | |
our @ISA; | |
use Animal; | |
@ISA = qw/Animal/; | |
sub new { | |
my $pkg = shift; | |
my $super = Animal->new(@_); | |
return bless {%$super, color => {@_}->{color}}, $pkg; | |
} | |
sub color { | |
my $self = shift; | |
return $self->{color}; | |
} | |
1; |
This file contains 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
my class Animal { | |
has Str $!name; | |
submethod BUILD(:$!name) {} | |
method name { | |
return $!name; | |
} | |
} | |
my class Bird is Animal { | |
has Str $!color; | |
submethod BUILD(:$!color) {} | |
method color { | |
return $!color; | |
} | |
} | |
my $animal = Animal.new(name => 'foo'); | |
say $animal.name; # foo | |
my $bird = Bird.new(name => 'bar', color => 'red'); | |
say $bird.name; # bar | |
say $bird.color; # red |
This file contains 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
my class Animal { | |
submethod BUILD { | |
say 'An Animal'; | |
} | |
} | |
my class Bird is Animal { | |
submethod BUILD { | |
say 'A Bird'; | |
} | |
} | |
Animal.new; # An Animal | |
Bird.new; # An Animal; A Bird |
This file contains 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
#!/bin/env perl | |
use feature qw/say/; | |
use Animal; | |
use Bird; | |
my $animal = Animal->new(name => 'foo'); | |
say $animal->name; # foo | |
my $bird = Bird->new(name => 'foo', color => 'red'); | |
say $bird->name; # foo | |
say $bird->color; # red |
This file contains 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
my class Animal { | |
method new { | |
say 'An Animal'; | |
} | |
} | |
my class Bird is Animal { | |
method new { | |
say 'A Bird'; | |
} | |
} | |
Animal.new; # An Animal | |
Bird.new; # A Bird |
This file contains 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
my class Animal { | |
has Str $!name; | |
method new(:$name) { | |
my $self = self.bless; | |
$self!name($name); | |
return $self; | |
} | |
method !name($name) { | |
$!name = $name; | |
} | |
method name { | |
return $!name; | |
} | |
} | |
my class Bird is Animal { | |
has Str $!color; | |
method new(:$color) { | |
my $self = self.bless; | |
$self!color($color); | |
return $self; | |
} | |
method !color($color) { | |
$!color = $color; | |
} | |
method color { | |
return $!color; | |
} | |
} | |
my $animal = Animal.new(name => 'foo'); | |
say $animal.name; # foo | |
my $bird = Bird.new(name => 'bar', color => 'red'); | |
say $bird.name; # (Str) | |
say $bird.color; # red |
This file contains 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
my class Animal { | |
has Str $!name; | |
method name { | |
return $!name; | |
} | |
} | |
my class Bird is Animal { | |
has Str $!color; | |
method color { | |
return $!color; | |
} | |
} | |
my $animal = Animal.new(name => 'foo'); | |
say $animal.name; # (Str) | |
my $bird = Bird.new(name => 'bar', color => 'red'); | |
say $bird.name; # (Str) | |
say $bird.color; # (Str) |
This file contains 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
my class Animal { | |
has Str $.name; | |
} | |
my class Bird is Animal { | |
has Str $.color; | |
} | |
my $animal = Animal.new(name => 'foo'); | |
say $animal.name; # foo | |
my $bird = Bird.new(name => 'bar', color => 'red'); | |
say $bird.name; # bar | |
say $bird.color; # red |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment