Menu
Coddy logo textTech

Récapitulatif : Définir des structures de tables

Fait partie de la section Introduction à Luau du Journey Lua de Coddy. Leçon 44 sur 73.

challenge icon

Défi

Moyen

Crée un alias de type nommé Computer avec :

  • id de type number
  • brand de type string
  • ramInGB de type number? (facultatif)
  • boot : une méthode (self: Computer) -> ()

Crée deux ordinateurs (associe boot avec la syntaxe de définition utilisant deux-points) :

  • workstation : id 1001, marque "Dell", ramInGB 16 ; son boot affiche [brand] is booting up...
  • laptop : id 1002, marque "MacBook", sans ramInGB ; son boot affiche [brand] is starting...

Crée une fonction specs qui prend un Computer et renvoie une string : [brand] with [ramInGB] GB of RAM lorsque la RAM est connue, sinon [brand] with unknown RAM.

Ensuite, dans l’ordre :

  1. appelle workstation:boot()
  2. appelle laptop:boot()
  3. affiche specs(workstation)
  4. affiche specs(laptop)
  5. affiche l’id de workstation

Essayez vous-même

-- Écrivez le code ici
-- 1) définir le type Computer (ramInGB optionnel, méthode boot)
-- 2) créer workstation et laptop, attacher boot avec la syntaxe deux-points
-- 3) écrire specs avec une vérification nil sur ramInGB
-- 4) boot les deux, afficher les deux specs, afficher workstation.id

Toutes les leçons de Introduction à Luau

Entraînez-vous par vous-même : Compilateur Lua en ligne