Last active
September 19, 2019 10:58
-
-
Save dpossas/67a288fb1302d99d48a58ce9eb79fac3 to your computer and use it in GitHub Desktop.
Conceitos Básicos de DART - Douglas Bezerra Possas
This file contains hidden or 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
main() { | |
final shopping = new ShoppingCart(); | |
//o uso da palavra reservada new é opcional a partir da DART 2.0 | |
//e não usaremos nas próximas linhas | |
final pen = Product("Caneta", true, 4.75); | |
shopping.addItem(pen); | |
final pencil = Product("Lápis", false, 1.25); | |
shopping.addItem(pencil); | |
print("Total da compra: ${shopping.total()}"); | |
} | |
class Product { | |
//Comentário | |
//Atributos com nível de privacidade privada | |
int _id; | |
String _title; | |
bool _active; | |
double _price; | |
//Definição de função via construtor | |
Product(this._title, this._active, this._price); | |
//Definição de função via getter | |
int get id => _id; | |
double get price => _price; | |
bool get active => _active; | |
String get title => _title; | |
} | |
class ShoppingCart { | |
List<Product> _products = []; | |
List<Product> get products => this._products; | |
void addItem(Product product){ | |
this.products.add(product); | |
} | |
double total() { | |
double total = 0; | |
this.products.forEach((Product p) { | |
total += p.price; | |
}); | |
return total; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment