Objects and Inheritance
Objects in Quetzal allow grouping data and behaviors. They are defined with the objeto keyword followed by the name and a block of members.
Basic Declaration
Section titled “Basic Declaration”objeto Usuario { texto nombre entero edad
Usuario(texto nombre, entero edad) { ambiente.nombre = nombre ambiente.edad = edad }
texto obtener_nombre() { retornar ambiente.nombre }}- Attributes declared outside
privadoorpublicoare public by default. - Constructors use the same object name and don’t return an explicit value.
Instantiation
Section titled “Instantiation”Usuario persona = nuevo Usuario("Maria", 30)texto nombre = persona.obtener_nombre()privado and publico Blocks
Section titled “privado and publico Blocks”objeto Banco { privado: numero var saldo = 0
publico: Banco(numero inicial) { ambiente.saldo = inicial }
numero obtener_saldo() { retornar ambiente.saldo }}- Members within
privadoare only available inside the object. - Members in
publicocan be used from anywhere.
Static Members (libre)
Section titled “Static Members (libre)”Members declared with libre act as static members: they can be invoked without creating an instance.
objeto Matematicas { libre numero PI = 3.14159
libre numero sumar(numero a, numero b) { retornar a + b }}
numero resultado = Matematicas.sumar(2, 3)Inheritance
Section titled “Inheritance”Quetzal supports object inheritance using the como keyword.
Simple Inheritance
Section titled “Simple Inheritance”objeto Animal { privado: texto var nombre publico: Animal(texto nombre) { ambiente.nombre = nombre }
texto obtener_nombre() { retornar ambiente.nombre }}
objeto Perro como Animal { publico: Perro(texto nombre) { padre.Animal(nombre) }
texto ladrar() { retornar "Guau!" }}Cascade Inheritance
Section titled “Cascade Inheritance”objeto Mamifero como Animal { publico: Mamifero(texto nombre) { padre.Animal(nombre) }}
objeto Gato como Mamifero { publico: Gato(texto nombre) { padre.Mamifero(nombre) }
texto maullar() { retornar "Miau!" }}Multiple Inheritance
Section titled “Multiple Inheritance”Quetzal allows inheriting from multiple parent objects by separating them with commas:
objeto Felino como Mamifero, Animal { publico: Felino(texto nombre) { padre.Mamifero(nombre) padre.Animal(nombre) }}Method Overriding
Section titled “Method Overriding”objeto Perro como Animal { publico: // Override parent method texto describir() { retornar "Soy un perro" }}