T
Thens
I have a base class called as Weapon and I have derived classes 'Gun'
and 'RocketLauncher'. I need to instantiate Gun or RocketLauncher
based on a parameter say distance from the target. If the distance is
less than 20m I need a gun and if it is more I will need a
RocketLauncher.
This is my attempt at a Perl code that does this. Is this the right
way to do this. Any pointers will be of help.
#! /usr/local/bin/perl -w
package Weapon;
use strict;
sub new {
my ( $self ) = @_;
my $class = ref($self) || $self;
return bless { }, $class;
}
sub hitPoints {
my ( $self ) = @_;
return $self->{hitpoints};
}
sub fire {
my ( $self, $target ) = @_;
print "Destroyed the target !! ", $self->hitPoints;
#$target->hurt($self->hitPoints);
}
package Gun;
use strict;
use base 'Weapon';
sub new {
my ( $self ) = @_;
my $class = ref($self) || $self;
$self = new Weapon();
$self->{type} = 'GUN';
$self->{hitpoints} = 10;
return $self;
}
package RocketLauncher;
use strict;
use base 'Weapon';
sub new {
my ( $self ) = @_;
my $class = ref($self) || $self;
$self = new Weapon();
$self->{type} = 'ROCKET';
$self->{hitpoints} = 100;
return $self;
}
1;
and 'RocketLauncher'. I need to instantiate Gun or RocketLauncher
based on a parameter say distance from the target. If the distance is
less than 20m I need a gun and if it is more I will need a
RocketLauncher.
This is my attempt at a Perl code that does this. Is this the right
way to do this. Any pointers will be of help.
#! /usr/local/bin/perl -w
package Weapon;
use strict;
sub new {
my ( $self ) = @_;
my $class = ref($self) || $self;
return bless { }, $class;
}
sub hitPoints {
my ( $self ) = @_;
return $self->{hitpoints};
}
sub fire {
my ( $self, $target ) = @_;
print "Destroyed the target !! ", $self->hitPoints;
#$target->hurt($self->hitPoints);
}
package Gun;
use strict;
use base 'Weapon';
sub new {
my ( $self ) = @_;
my $class = ref($self) || $self;
$self = new Weapon();
$self->{type} = 'GUN';
$self->{hitpoints} = 10;
return $self;
}
package RocketLauncher;
use strict;
use base 'Weapon';
sub new {
my ( $self ) = @_;
my $class = ref($self) || $self;
$self = new Weapon();
$self->{type} = 'ROCKET';
$self->{hitpoints} = 100;
return $self;
}
1;