Created
January 16, 2017 09:46
-
-
Save MaximePawlakFr/0370f06181f4676f8aa990b8ac5d91f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? php | |
class Voiture{ | |
// Attributs | |
private $_marque = "Renault"; | |
private $_puissance = 0; | |
private $_kilometrage = 0; | |
private $_vitesse = 0; | |
// Méthodes | |
function rouler(){ | |
} | |
function demarrer(){ | |
$this->_vitesse = $this->_vitesse + 10; | |
} | |
// Getter | |
public function getVitesse(){ | |
return $this->_vitesse; | |
} | |
// Setter | |
public function setVitesse($newVitesse){ | |
if($newVitesse >= 0){ | |
$this->_vitesse = $newVitesse; | |
} | |
} | |
} | |
$maVoiture = new Voiture; | |
$maVoiture->demarrer(); // vitesse = 10 | |
$maTwingo = new Voiture; //vitesse = 0 | |
$maVoiture->demarrer(); // vitesse = 20 | |
// $maTwingo : vitesse = 0; | |
class MachineACafe { | |
// Encapsulation | |
private $_quantite = 20; | |
private $_sucre = 5; | |
public function cafeNormal(){ | |
// ... verser le café | |
// $this->_sucre; // 5 | |
// $this->_quantite ++; | |
}; | |
private function cafeNormalAvecSucre($newSugar){ | |
// ... verser le café | |
// Verser $newSugar grammes de sucre | |
// $this->_quantite ++; | |
}; | |
function getQuantite(){ | |
return $this->_quantite; | |
} | |
/* function setQuantite($newQuantite){ | |
$this->_quantite = $newQuantite; | |
} | |
*/ | |
} | |
$machineACafeIOTValley = new MachineACafe; | |
$machineACafeIOTValley.cafeNormal(); | |
// IMPOSSIBLE d'utiliser $_quantite; | |
$machineACafeIOTValley.getQuantite(); | |
$machineACafeIOTValley.cafeNormalAvecSucre(0); | |
$machineACafeIOTValley.cafeNormalAvecSucre(10); | |
class Ferrari extends Voiture { | |
$_couleur = "rouge"; | |
function roulerA300(){ | |
$this->setVitesse(300); | |
} | |
$this->_kilometrage // ERROR | |
$this->getKilometrage() | |
$this->getVitesse(); | |
} | |
$maFerrari = new Ferrari; | |
$maFerrari.getVitesse(); // 0 | |
$maFerrari.roulerA300(); | |
$maFerrari.getVitesse();// 300 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment