Menu
Coddy logo textTech

Riepilogo: definire le forme delle tabelle

Fa parte della sezione Introduzione a Luau del percorso Lua di Coddy. Lezione 44 di 73.

challenge icon

Sfida

Medio

Crea un alias di tipo chiamato Computer con:

  • id di tipo number
  • brand di tipo string
  • ramInGB di tipo number? (opzionale)
  • boot: un metodo (self: Computer) -> ()

Crea due computer (assegna boot usando la sintassi di definizione con i due punti):

  • workstation: id 1001, brand "Dell", ramInGB 16; il suo boot stampa [brand] is booting up...
  • laptop: id 1002, brand "MacBook", nessun ramInGB; il suo boot stampa [brand] is starting...

Crea una funzione specs che accetta un Computer e restituisce una string: [brand] with [ramInGB] GB of RAM quando la RAM è nota, altrimenti [brand] with unknown RAM.

Poi, nell'ordine:

  1. chiama workstation:boot()
  2. chiama laptop:boot()
  3. stampa specs(workstation)
  4. stampa specs(laptop)
  5. stampa l'id di workstation

Provalo tu

-- Scrivi il codice qui
-- 1) definisci il tipo Computer (ramInGB facoltativo, metodo boot)
-- 2) crea workstation e laptop, associa boot usando la sintassi con i due punti
-- 3) scrivi specs con un controllo di nil su ramInGB
-- 4) avvia entrambi, stampa entrambe le specs, stampa workstation.id

Tutte le lezioni di Introduzione a Luau

Esercitati da solo: Compilatore Luau online