JavaScript klases mantojums

Šajā apmācībā jūs uzzināsit par JavaScript klases mantošanu, izmantojot piemērus.

Klases mantojums

Mantošana ļauj definēt klasi, kas pārņem visu vecāku klases funkcionalitāti, un ļauj pievienot vairāk.

Izmantojot klases mantojumu, klase var mantot visas citas klases metodes un īpašības.

Mantošana ir noderīga funkcija, kas ļauj atkārtoti izmantot kodu.

Lai izmantotu klases mantojumu, izmantojiet extendsatslēgvārdu. Piemēram,

 // parent class class Person ( constructor(name) ( this.name = name; ) greet() ( console.log(`Hello $(this.name)`); ) ) // inheriting parent class class Student extends Person ( ) let student1 = new Student('Jack'); student1.greet();

Rezultāts

 Sveiks Džek

Iepriekš minētajā piemērā Studentklase pārmanto visas klases metodes un īpašības Person. Tādējādi Studentklasei tagad būs nameīpašums un greet()metode.

Pēc tam mēs piekļuvām klases greet()metodei, Studentizveidojot student1objektu.

JavaScript super () atslēgvārds

superAtslēgvārds izmanto iekšpuses bērnu klasi apzīmē tās mātes klasi. Piemēram,

 // parent class class Person ( constructor(name) ( this.name = name; ) greet() ( console.log(`Hello $(this.name)`); ) ) // inheriting parent class class Student extends Person ( constructor(name) ( console.log("Creating student class"); // call the super class constructor and pass in the name parameter super(name); ) ) let student1 = new Student('Jack'); student1.greet();

Šeit superiekšējā Studentklase attiecas uz Personklasi. Tādējādi, ja Studenttiek izsaukts klases konstruktors , tas izsauc arī Personklases konstruktoru, kas tam piešķir nosaukuma rekvizītu.

Svarīgākā metode vai īpašums

Ja bērnu klasei ir tāda pati metode vai rekvizīta nosaukums kā vecāku klasei, tā izmantos bērnu klases metodi un rekvizītu. Šo jēdzienu sauc par metodes ignorēšanu. Piemēram,

 // parent class class Person ( constructor(name) ( this.name = name; this.occupation = "unemployed"; ) greet() ( console.log(`Hello $(this.name).`); ) ) // inheriting parent class class Student extends Person ( constructor(name) ( // call the super class constructor and pass in the name parameter super(name); // Overriding an occupation property this.occupation = 'Student'; ) // overriding Person's method greet() ( console.log(`Hello student $(this.name).`); console.log('occupation: ' + this.occupation); ) ) let p = new Student('Jack'); p.greet();

Rezultāts

Sveiks, students Džek. nodarbošanās: Students

Šeit occupationīpašums un greet()metode ir vecāku Personklasē un bērnu Studentklasē. Tādējādi Studentklase ignorē occupationrekvizītu un greet()metodi.

Mantojuma izmantošana

  • Tā kā bērnu klase var mantot visas vecāku klases funkcijas, tas ļauj kodu atkārtoti izmantot.
  • Kad funkcionalitāte ir izstrādāta, varat to vienkārši pārmantot. Nav nepieciešams no jauna izgudrot riteni. Tas ļauj tīrāku kodu un vieglāk uzturēt.
  • Tā kā bērnu klasē varat pievienot arī savas funkcijas, varat mantot tikai noderīgās funkcijas un definēt citas nepieciešamās funkcijas.

Interesanti raksti...