-
Notifications
You must be signed in to change notification settings - Fork 0
/
exo14.php
132 lines (100 loc) · 3.04 KB
/
exo14.php
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<h2>Exercice 14</h2>
<p>
Créer une classe Voiture possédant 2 propriétés (marque et modèle) ainsi qu'une classe VoitureElec<br>
qui hérite (extends) de la classe Voiture et qui a une copriété supplémentaire (autonomoie).<br>
Instancier une voiture "classique" et une voiture "electrique" ayant les caractéristiques suivantes :<br>
- Peugeot 408:$v1 = new Voiture("Peugeot","408");<br>
- BMW i3 150:$ve1 = new VoitureElec("BMW","I3",100);<br>
Votre programme de test devra afficher les informations des 2 voitures de la façon suivante :<br>
echo $v1->getInfos()."<br/>";<br>
echo $ve1->getInfos()."<br/>";<br>
</p>
<?php
class Voiture {
protected string $marque;
protected string $modele;
public function __construct(string $marque, string $modele) {
$this->marque = $marque;
$this->modele = $modele;
}
public function getMarque()
{
return $this->marque;
}
public function setMarque($marque)
{
$this->marque = $marque;
return $this;
}
public function getModele()
{
return $this->modele;
}
public function setModele($modele)
{
$this->modele = $modele;
return $this;
}
public function getInfos() {
return "<h2>Infos véhicule</h2>
*********************<br>
<p>Marque : ". $this->marque."<br>
Modèle : ". $this->modele."<br>";
}
public function __toString() {
return $this->marque." ".$this->modele;
}
}
class VoitureElec extends Voiture {
private int $autonomie;
public function __construct(string $marque, string $modele, int $autonomie) {
parent::__construct($marque, $modele);
$this->autonomie = $autonomie;
}
public function getAutonomie()
{
return $this->autonomie;
}
public function setAutonomie($autonomie)
{
$this->autonomie = $autonomie;
return $this;
}
public function getInfos() {
return parent::getInfos()."
Autonomie : ".$this->autonomie."<br></p>";
}
public function __toString() {
parent::__toString();
}
}
$v1 = new Voiture("Peugeot", "408");
$ve1 = new VoitureElec("BMW", "I3", 100);
echo $v1->getInfos(). "<br>";
echo $ve1->getInfos(). "<br>";
/* FROM https://www.w3schools.com/PHP/php_oop_inheritance.asp
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public $weight;
public function __construct($name, $color, $weight) {
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
}
public function intro() {
echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
}
}
$strawberry = new Strawberry("Strawberry", "red", 50);
$strawberry->intro();
*/