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éez un alias de type nommé Computer avec :

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

Créez deux ordinateurs (attachez boot avec la syntaxe de définition avec deux-points) :

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

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

Ensuite, dans l'ordre :

  1. appelez workstation:boot()
  2. appelez laptop:boot()
  3. affichez specs(workstation)
  4. affichez specs(laptop)
  5. affichez l'id de workstation

Essayez vous-même

-- Écrire 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, imprimer les deux specs, imprimer workstation.id

Toutes les leçons de Introduction à Luau