Created
January 13, 2011 15:06
-
-
Save redtower/777995 to your computer and use it in GitHub Desktop.
オブジェクト指向とMooseとClass::Accesser
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 Class; | |
use base qw(Class::Accessor); | |
__PACKAGE__->mk_accessors(qw(X Y)); | |
1; | |
package Main; | |
my $o = Class->new({X => 20}); | |
print $o->X() . "/" . $o->Y() . "\n"; # 20/ | |
$o = Class->new({X => 40, Y => 50}); | |
print $o->X() . "/" . $o->Y() . "\n"; # 40/50 | |
$o->X(300); | |
print $o->X() . "/" . $o->Y() . "\n"; # 300/50 |
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 Class; | |
use Moose; | |
has X => (is => 'rw', isa => 'Int', default => 0); | |
has Y => (is => 'rw', isa => 'Int', default => 0); | |
1; | |
package Main; | |
my $o = Class->new(X => 20); | |
print $o->X() . "/" . $o->Y() . "\n"; # 20/0 | |
$o = Class->new(X => 40, Y => 50); | |
print $o->X() . "/" . $o->Y() . "\n"; # 40/50 | |
$o->X(300); | |
print $o->X() . "/" . $o->Y() . "\n"; # 300/50 |
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 Class; | |
sub new { | |
my $class = shift; | |
my $self = {X => shift, Y => shift,}; | |
return bless $self, $class; | |
} | |
sub x { | |
my $self = shift; | |
if (@_) { | |
$self->{'X'} = @_[0]; | |
} | |
return $self->{'X'}; | |
} | |
sub y { | |
my $self = shift; | |
if (@_) { | |
$self->{'Y'} = @_[0]; | |
} | |
return $self->{'Y'}; | |
} | |
1; | |
package Main; | |
my $o = Class->new(20); | |
print $o->x() . "/" . $o->y() . "\n"; # 20/ | |
$o = Class->new(40, 50); | |
print $o->x() . "/" . $o->y() . "\n"; # 40/50 | |
$o->x(300); | |
print $o->x() . "/" . $o->y() . "\n"; # 300/50 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment