面向对象编程 示例 日期默认设置、更改、显示

--包(类)源码

#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{

my $self=shift;

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

return $self->{the_mon};

}

sub day{

my $self=shift;

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

return $self->{the_day};

}


sub setDate{

if(@_==4){

my $self=shift;

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

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

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

}

else{

print "Error happend\n";

}

}


sub disp{

my $self=shift;

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


}

return 1;

---应用代码

wKiom1YMM1LRzy2bAAB-kq8eJBE204.jpg

--结果

wKioL1YMM4mzdbvWAACSANGI6o8134.jpg

你可能感兴趣的:(类)