Last active
July 4, 2019 10:12
-
-
Save nikli2009/f1664e206fefc5692b661adb6f377273 to your computer and use it in GitHub Desktop.
Dart Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
Car newMercedes = Car(brandName:'Benz', modelName:'G', currency:'USD', msrp:1000.0); | |
print('Car: ${newMercedes.brandName} ${newMercedes.modelName}'); | |
print('Price: ${newMercedes.currency} ${newMercedes.msrp}'); | |
print(''.padLeft(20, '-')); | |
Car newHonda = Car.shorter(brandName:'Honda', modelName:'Fit'); | |
print('Car: ${newHonda.brandName} ${newHonda.modelName}'); | |
print(''.padLeft(20, '-')); | |
newHonda.printModelName(); | |
// use static property | |
var Secret = Car.SECRET; | |
print(''.padLeft(20, '-')); | |
print('Static Property | Car Secret: $Secret'); | |
// use static method | |
print(''.padLeft(20, '-')); | |
print(Car.toCapitalCase(Secret)); | |
print(Car.toCapitalCase(Secret, false)); | |
// Object Memory Storage - Objects | |
print(''.padLeft(20, '-')); | |
var copyOfHonda = newHonda; | |
var copy2OfHonda = newHonda; | |
copyOfHonda.brandName = 'Point To Same Memory Storage Address'; | |
print('copyOfHonda - ${copyOfHonda.brandName}'); | |
print('copy2OfHonda - ${copy2OfHonda.brandName}'); | |
// Object Memory Storage - Primitives | |
print(''.padLeft(20, '-')); | |
var name = 'Nikk'; | |
var cName = name; | |
var cName1 = name; | |
cName = 'Johnny'; | |
print(cName); // Johnny | |
print(cName1); // Nikk | |
// Inheritence | |
print(''.padLeft(20, '-')); | |
var tesla = new ElectricCar(brandName: 'Tesla', modelName: 'X'); | |
print('ElectricCar: ${tesla.brandName} ${tesla.modelName}'); | |
print('Price: ${tesla.currency} ${tesla.msrp}'); | |
tesla.printModelName(); | |
print(ElectricCar.toCapitalCase('EC - toCapitalCase')); | |
// named constructor | |
print(''.padLeft(20, '-')); | |
var teslaShort = new ElectricCar.shorter(brandName: 'Tesla'); | |
print('ElectricCarShort: ${teslaShort.brandName} ${teslaShort.modelName}'); | |
} | |
class Car { | |
// static property | |
static const SECRET = 'niklee2009'; | |
// normal property | |
String brandName; | |
String modelName; | |
String currency; | |
double msrp; | |
Car({this.brandName, this.modelName, this.currency, this.msrp}){ | |
print('default constructor'); | |
} | |
// named constructor | |
Car.shorter({String brandName, String modelName}) : | |
this.brandName = brandName, | |
this.modelName = modelName { | |
print('named constructor'); | |
} | |
// methods | |
void printModelName() { | |
var brandName = 'Local Name'; | |
print('method executed - printModelName()'); | |
print('Car: ${brandName} ${modelName}'); | |
print('Car: ${this.brandName} ${modelName}'); | |
} | |
// static method | |
static String toCapitalCase(String original, [bool optionalParam = true]) { | |
if(original == null) { original = ''; } | |
return '${original.toUpperCase()} - ${optionalParam}'; | |
} | |
} | |
// Inheritance | |
class ElectricCar extends Car { | |
// call Parent Constructor and has child its own constructor | |
ElectricCar({ | |
String brandName, | |
String modelName, | |
String currency = 'USD', | |
double msrp = 500.00 | |
}) : | |
// parent constructor -> execute first | |
super( | |
brandName: brandName, | |
modelName: modelName, | |
currency: currency, | |
msrp: msrp) { | |
// child constructor -> execute after | |
print('Electric Car Constructor'); | |
} | |
// override named constructor | |
ElectricCar.shorter({ | |
String brandName, | |
String modelName = 'shorter model' | |
}) : | |
// parent constructor -> execute first | |
super( | |
brandName: brandName, | |
modelName: modelName) { | |
// child constructor -> execute after | |
print('Electric Car shorter Constructor'); | |
} | |
// override parent methods | |
// if we don't mark as override, the override work is still done. | |
@override | |
void printModelName() { | |
print(''.padLeft(20, '-')); | |
print('@Method Override'); | |
// execute parent method | |
super.printModelName(); | |
// execute child method | |
var brandName = 'Local Name'; | |
print('method executed - Electric printModelName()'); | |
print('Electric Car: ${brandName} ${modelName}'); | |
print('Electric Car: ${this.brandName} ${modelName}'); | |
} | |
// static method | |
// static method doesn't inherit, but you can still override it without @override | |
static String toCapitalCase(String original, [bool optionalParam = false]) { | |
// super.toCapitalCase('Parent'); ---> error, super is not available | |
if(original == null) { original = 'Electric Static Method'; } | |
return '${original.toUpperCase()} - ${optionalParam}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment