Skip to content

Instantly share code, notes, and snippets.

@pascencio
Created April 22, 2020 19:33
Show Gist options
  • Save pascencio/389c57b936302b3c9b9d73373440fb84 to your computer and use it in GitHub Desktop.
Save pascencio/389c57b936302b3c9b9d73373440fb84 to your computer and use it in GitHub Desktop.
Dart Inheritance
import 'dart:convert';
void main() {
print('Raw JSON: $rawJson');
final Map jsonMap = json.decode(rawJson);
print('JSON Map: $jsonMap');
final persona = Ciudadano.fromMap(jsonMap);
persona.describir();
persona.peso = 84.7;
persona.edad = 78;
persona.jubilado = true;
print('Información de la persona del futuro: \n $persona');
if (persona.jubilado) {
print('Felicitaciones esta Jubilado! ...¿Su pensión sera buena?');
}
persona.dormir();
}
final String rawJson = '''
{
"nombre": "Patricio",
"edad": 37,
"peso": 96.7,
"casado": true,
"hombre": true,
"hijos": {
"1": {
"nombre": "Joaquin",
"edad": 11
},
"2": {
"nombre": "Maximiliano",
"edad": 8
}
}
}''';
class Ciudadano extends Persona implements Mamifero {
bool _jubilado = false;
set jubilado(bool jubilado) {
this._jubilado = jubilado;
}
get jubilado {
return this._jubilado;
}
void dormir(){
print("Durmiendo ...Zzzzzzz");
}
void describir() {
final List<String> nombresDeHijos = [this._hijos[1]['nombre']];
nombresDeHijos.add(this._hijos[2]['nombre']);
final List<int> numerosDeLaSuerte = List(10);
for (var i = 0; i < numerosDeLaSuerte.length; i++) {
numerosDeLaSuerte[i] = (i * 2);
}
final String mensaje = '''Hola ${this._nombre}
Tu edad es: ${this._edad}
Tu peso es: ${this._peso}
La primera letra de tu nombre es: ${this._nombre[0]}
Tu estado civil es: ${this._casado ? 'Casado' : 'Soltero'}
Tu no eres: ${!this._hombre ? 'Hombre' : 'Mujer'}
¿Estas jubilado?: ${this._jubilado ? 'Si' : 'No'}
Tus hijos son: $nombresDeHijos
La edad de tu primer hijo es: ${this._hijos[1]['edad']}
Tus numeros de la suerte son: $numerosDeLaSuerte''';
print(mensaje);
}
String convertirAString() => """Persona(
nombre: '${this._nombre}',
edad: ${this._edad},
peso: ${this._peso},
jubilado: ${this._jubilado}
)""";
Ciudadano.fromMap(Map jsonMap) {
_nombre = jsonMap['nombre'];
_edad = jsonMap['edad'];
_peso = jsonMap['peso'];
_casado = jsonMap['casado'];
_hombre = jsonMap['hombre'];
_hijos = Map();
Map hijosMap = jsonMap['hijos'];
for (var k in hijosMap.keys) {
int i = int.parse(k);
_hijos[i] = hijosMap[k];
}
_jubilado = false;
}
}
abstract class Mamifero {
void dormir();
}
abstract class Persona {
String _nombre;
int _edad;
double _peso;
bool _casado;
bool _hombre;
Map<int, Map> _hijos;
Persona();
set edad(int edad) {
this._edad = edad;
}
set peso(double peso) {
this._peso = peso;
}
void describir();
String convertirAString();
String toString() => convertirAString();
Persona.fromMap(Map jsonMap);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment