Informatique


L'héritage

Aller à

L'héritage

Le mot clé extends indique qu'une classe fille hérite d'une classe mère, la méthode super appelle le constructeur de la classe mère

Démo
	<h2>Héritage à partir d'une classe</h2>
	<script>
class Animal { 
//La méthode constructor
	constructor(nom,sexe,age){
		this.nom=nom;
		this.sexe=sexe;
		this.age=age;
		this.photo=this.nom+".jpg";
	
	}
	affic(){
		var s= (this.sex==="F")? "Femelle":"Mâle";
		return "<div>"+s+" - "+this.nom+" - "+this.age+" ans"+" - "+this.photo+"</div>";
	}	
}

class Chien extends Animal {
	constructor(nom,sexe,age) {
		// Héritage de la classe Animal
		super(nom,sexe,age);
		//Propriétés caractéristiques du chien
		this.cri="Aboiement";
		this.male="Chien";		
		this.femelle="Chienne";
	}
}
// instanciation
var Stich = new Chien("Stich","M","5");
console.log(Stich);
document.write(Stich.affic()); 
	</script>

On constate dans la console: