tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
53
Task/Polymorphic-copy/Perl/polymorphic-copy.pl
Normal file
53
Task/Polymorphic-copy/Perl/polymorphic-copy.pl
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package T;
|
||||
sub new {
|
||||
my $cls = shift;
|
||||
bless [ @_ ], $cls
|
||||
}
|
||||
|
||||
sub set_data {
|
||||
my $self = shift;
|
||||
@$self = @_;
|
||||
}
|
||||
|
||||
sub copy {
|
||||
my $self = shift;
|
||||
bless [ @$self ], ref $self;
|
||||
}
|
||||
|
||||
sub manifest {
|
||||
my $self = shift;
|
||||
print "type T, content: @$self\n\n";
|
||||
}
|
||||
|
||||
package S;
|
||||
our @ISA = 'T';
|
||||
# S is inheriting from T.
|
||||
# 'manifest' method is overriden, while 'new', 'copy' and
|
||||
# 'set_data' are all inherited.
|
||||
sub manifest {
|
||||
my $self = shift;
|
||||
print "type S, content: @$self\n\n";
|
||||
}
|
||||
|
||||
package main;
|
||||
|
||||
print "# creating \$t as a T\n";
|
||||
my $t = T->new('abc');
|
||||
$t->manifest;
|
||||
|
||||
print "# creating \$s as an S\n";
|
||||
my $s = S->new('SPQR');
|
||||
$s->manifest;
|
||||
|
||||
print "# make var \$x as a copy of \$t\n";
|
||||
my $x = $t->copy;
|
||||
$x->manifest;
|
||||
|
||||
print "# now as a copy of \$s\n";
|
||||
$x = $s->copy;
|
||||
$x->manifest;
|
||||
|
||||
print "# show that this copy is indeed a separate entity\n";
|
||||
$x->set_data('totally different');
|
||||
print "\$x is: ";
|
||||
$x->manifest;
|
||||
Loading…
Add table
Add a link
Reference in a new issue