Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Advanced Dart
- * https://www.tutorialspoint.com/dart_programming/dart_programming_html_dom.htm
- * https://dartpad.dartlang.org/
- */
- /*
- * Package Manager: pub (can download Various Libraries)
- * Package Metadata: pubsec.yaml (contains dependencies)
- * YAML = Yet Another Markup Language
- */
- import 'dart:collection' show Queue; // To use Queue (Only import Queue)
- import 'dart:io';
- import 'dart:html';
- /*
- * _ in _identifier means private (Encapsulation)
- */
- typedef PrintSymbol(String _string);
- DollarSign(String _string) {
- print('${_string}\$');
- }
- HashtagSign(String _string) {
- print('${_string}#');
- }
- AmperstandSign(String _string) {
- print('${_string}&');
- }
- SignOperator(String _string, PrintSymbol sym) {
- sym(_string);
- }
- class Animal {
- String name;
- int age;
- Animal() {
- print("A new animal is being created");
- }
- // Multiple Constructor
- Animal.namedConst(String name) {
- this.name = name;
- print('${name} is an animal now');
- }
- void walk() {
- print('Animal is walking');
- }
- }
- class LivingBeing {
- void breathe() {
- print('Living Being is breathing');
- }
- }
- class Mammal implements Animal, LivingBeing {
- // Multiple interfaces is ok
- @override
- int age;
- @override
- String name;
- @override
- void walk() {
- print('Mammal is walking');
- }
- @override
- void breathe() {
- print('Mammal is breathing');
- }
- }
- class Human extends LivingBeing {
- // Single Inheritance
- // There is no multiple inheritance (multi-level is ok)
- String name;
- static int totalHuman;
- Human() {
- super.breathe();
- }
- String get humanName {
- return name;
- }
- void set humanName(name) {
- this.name = name;
- }
- }
- void main() {
- int _int;
- String _string;
- // Dart is OOP
- Animal dog = new Animal.namedConst("Doge");
- print(dog.name);
- dog.age = 5;
- dog.walk();
- print(dog.age);
- print("");
- // Interfaces
- Mammal cow = new Mammal();
- cow.breathe();
- print("");
- // Static
- Human.totalHuman = 0;
- // Super
- Human john = new Human();
- // Setter & Getter
- john.humanName = "John Doe";
- print(john.humanName);
- // Inheritance
- john.breathe();
- Human.totalHuman++;
- // Cascade Operator (Sequence Call via Object)
- cow..walk()..breathe();
- print('');
- /*
- * Dart has no Array but Collection
- * - List
- * - Set (Has unique Value)
- * - Maps (Key : Value)
- * - Queue (First in, First out)
- */
- Set _set = new Set();
- _set.add('x');
- _set.add('y');
- _set.add('z');
- _set.add('x'); // Unique, will be skipped
- print('Set = ${_set}');
- // Queue should import 'dart:collection'
- Queue _queue = new Queue();
- _queue.add(10);
- _queue.add(20);
- _queue.add(30);
- _queue.add(40);
- print('Queue = ${_queue}');
- Iterator _iterator = _queue.iterator;
- while (_iterator.moveNext()) {
- print('Queue interator = ${_iterator.current}');
- }
- print('');
- // Try Catch
- try {
- _int = 5 ~/ 0;
- }
- catch (e) {
- print(e);
- print('Cannot divide by zero');
- }
- _int = -5;
- try {
- print(foo(_int));
- }
- catch (e) {
- print('Throw: ${e}');
- }
- print("");
- // Typedef
- _string = "This is ";
- PrintSymbol sym;
- sym = DollarSign;
- sym(_string);
- sym = HashtagSign;
- sym(_string);
- sym = AmperstandSign;
- sym(_string);
- SignOperator(_string, DollarSign);
- sym(_string);
- print("");
- // Input
- print('Input: ');
- try {
- _string = stdin.readLineSync();
- } catch (e) {
- print(e);
- }
- print(_string);
- // HTML
- var div = querySelector('div');
- div.text = "The text changed";
- }
- foo(int _int) {
- // Return Type not specified = dynamic
- if (_int < 0) {
- throw new FormatException();
- }
- return _int;
- }
Add Comment
Please, Sign In to add comment