-
Notifications
You must be signed in to change notification settings - Fork 1
/
TestPersonne.pl
50 lines (42 loc) · 1.1 KB
/
TestPersonne.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env perl
use 5.24.1;
use experimental qw( signatures );
package Personne;
sub new ($class, $nom, $prenom){
my $this = {
nom => $nom,
prenom => $prenom
};
bless($this,$class);
return $this;
}
sub toString ($this){
return "$this->{nom} $this->{prenom}";
}
package Etudiant;
our @ISA = qw(Personne);
our $NB = 0;
sub new ($class, $nom, $prenom, $num){
my $this = $class->SUPER::new($nom, $prenom);
$this->{num} = $num;
bless($this, $class);
++$NB;
return $this;
}
sub toString ($this){
my $parent = $this->SUPER::toString();
return "$parent Student $this->{num}";
}
# Une instance
my $p = Personne->new('Duchemin ', 'paul');
say $p->toString();
# Une Collection
my @al1 = map { Personne->new("Duchemin$_ ", "paul$_ ")} my @numbers = (1..10);
print map{$_->toString() . "\n"} @al1;
# Polymorphisme
my @al2 = map {
if ($_ % 2 == 0) {
Personne->new("Duchemin$_ ", "paul$_ ")}
else {
Etudiant->new("Durand$_", "jules$_", $_)} } my @numbers = (1..10);
print map{$_->toString() . "\n"} @al2; say "Nombre d'étudiants: $Etudiant::NB";