天天看點

面向對象程式設計 示例 日期預設設定、更改、顯示

--包(類)源碼

#Date.pm

package Date;

sub new{

my $date={

the_year=>2015,

the_mon=>10,

the_day=>1,

};

bless($date);

return $date;

}

sub year{

my $self=shift;

$self->{the_year}=shift if(@_);

return $self->{the_year};

sub mon{

$self->{the_mon}=shift if(@_);

return $self->{the_mon};

sub day{

$self->{the_day}=shift if(@_);

return $self->{the_day};

sub setDate{

if(@_==4){

$self->mon($_[0]);

$self->day($_[1]);

$self->year($_[2]);

else{

print "Error happend\n";

sub disp{

print $self->mon."-".$self->day."-".$self->year."\n";

return 1;

---應用代碼

<a href="http://s3.51cto.com/wyfs02/M01/74/02/wKiom1YMM1LRzy2bAAB-kq8eJBE204.jpg" target="_blank"></a>

--結果

<a href="http://s3.51cto.com/wyfs02/M01/73/FF/wKioL1YMM4mzdbvWAACSANGI6o8134.jpg" target="_blank"></a>

本文轉自 tiger506 51CTO部落格,原文連結:http://blog.51cto.com/tiger506/1699708,如需轉載請自行聯系原作者