ApelMerah

Learning Dart [Advanced]

Aug 15th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.84 KB | None | 0 0
  1. /*
  2.  * Advanced Dart
  3.  * https://www.tutorialspoint.com/dart_programming/dart_programming_html_dom.htm
  4.  * https://dartpad.dartlang.org/
  5.  */
  6.  
  7. /*
  8.  * Package Manager: pub (can download Various Libraries)
  9.  * Package Metadata: pubsec.yaml (contains dependencies)
  10.  * YAML = Yet Another Markup Language
  11.  */
  12.  
  13. import 'dart:collection' show Queue;    // To use Queue (Only import Queue)
  14. import 'dart:io';
  15. import 'dart:html';
  16.  
  17. /*
  18.  * _ in _identifier means private (Encapsulation)
  19.  */
  20.  
  21. typedef PrintSymbol(String _string);
  22.  
  23. DollarSign(String _string) {
  24.   print('${_string}\$');
  25. }
  26.  
  27. HashtagSign(String _string) {
  28.   print('${_string}#');
  29. }
  30.  
  31. AmperstandSign(String _string) {
  32.   print('${_string}&');
  33. }
  34.  
  35. SignOperator(String _string, PrintSymbol sym) {
  36.   sym(_string);
  37. }
  38.  
  39. class Animal {
  40.   String name;
  41.   int age;
  42.  
  43.   Animal() {
  44.     print("A new animal is being created");
  45.   }
  46.  
  47.   // Multiple Constructor
  48.   Animal.namedConst(String name) {
  49.     this.name = name;
  50.     print('${name} is an animal now');
  51.   }
  52.  
  53.   void walk() {
  54.     print('Animal is walking');
  55.   }
  56. }
  57.  
  58. class LivingBeing {  
  59.   void breathe() {
  60.     print('Living Being is breathing');
  61.   }
  62. }
  63.  
  64. class Mammal implements Animal, LivingBeing {
  65.   // Multiple interfaces is ok
  66.   @override
  67.   int age;
  68.  
  69.   @override
  70.   String name;
  71.  
  72.   @override
  73.   void walk() {
  74.     print('Mammal is walking');
  75.   }
  76.  
  77.   @override
  78.   void breathe() {
  79.     print('Mammal is breathing');
  80.   }
  81.  
  82. }
  83.  
  84. class Human extends LivingBeing {
  85.   // Single Inheritance
  86.   // There is no multiple inheritance (multi-level is ok)
  87.   String name;
  88.   static int totalHuman;
  89.  
  90.   Human() {
  91.     super.breathe();
  92.   }
  93.  
  94.   String get humanName {
  95.     return name;
  96.   }
  97.  
  98.   void set humanName(name) {
  99.     this.name = name;
  100.   }
  101. }
  102.  
  103. void main() {
  104.   int _int;
  105.   String _string;
  106.  
  107.   // Dart is OOP
  108.   Animal dog = new Animal.namedConst("Doge");
  109.   print(dog.name);
  110.   dog.age = 5;
  111.   dog.walk();
  112.   print(dog.age);
  113.   print("");
  114.  
  115.   // Interfaces
  116.   Mammal cow = new Mammal();
  117.   cow.breathe();
  118.   print("");
  119.  
  120.   // Static
  121.   Human.totalHuman = 0;
  122.   // Super
  123.   Human john = new Human();
  124.   // Setter & Getter
  125.   john.humanName = "John Doe";
  126.   print(john.humanName);
  127.   // Inheritance
  128.   john.breathe();
  129.   Human.totalHuman++;
  130.  
  131.   // Cascade Operator (Sequence Call via Object)
  132.   cow..walk()..breathe();
  133.   print('');
  134.  
  135.   /*
  136.    * Dart has no Array but Collection
  137.    *    - List
  138.    *    - Set (Has unique Value)
  139.    *    - Maps (Key : Value)
  140.    *    - Queue (First in, First out)
  141.    */
  142.   Set _set = new Set();
  143.   _set.add('x');
  144.   _set.add('y');
  145.   _set.add('z');
  146.   _set.add('x');    // Unique, will be skipped
  147.  
  148.   print('Set = ${_set}');
  149.  
  150.   // Queue should import 'dart:collection'
  151.   Queue _queue = new Queue();
  152.   _queue.add(10);
  153.   _queue.add(20);
  154.   _queue.add(30);
  155.   _queue.add(40);
  156.  
  157.   print('Queue = ${_queue}');
  158.  
  159.   Iterator _iterator = _queue.iterator;
  160.  
  161.   while (_iterator.moveNext()) {
  162.     print('Queue interator = ${_iterator.current}');
  163.   }
  164.   print('');
  165.  
  166.   // Try Catch
  167.   try {
  168.     _int = 5 ~/ 0;
  169.   }
  170.   catch (e) {
  171.     print(e);
  172.         print('Cannot divide by zero');
  173.     }
  174.  
  175.   _int = -5;
  176.  
  177.   try {
  178.     print(foo(_int));
  179.   }
  180.   catch (e) {
  181.     print('Throw: ${e}');
  182.   }
  183.   print("");
  184.  
  185.   // Typedef
  186.   _string = "This is ";
  187.   PrintSymbol sym;
  188.   sym = DollarSign;
  189.   sym(_string);
  190.   sym = HashtagSign;
  191.   sym(_string);
  192.   sym = AmperstandSign;
  193.   sym(_string);
  194.     SignOperator(_string, DollarSign);
  195.   sym(_string);
  196.   print("");
  197.  
  198.   // Input
  199.   print('Input: ');
  200.   try {
  201.     _string = stdin.readLineSync();
  202.   } catch (e) {
  203.     print(e);
  204.   }
  205.   print(_string);
  206.  
  207.   // HTML
  208.   var div = querySelector('div');
  209.   div.text = "The text changed";
  210. }
  211.  
  212. foo(int _int) {
  213.   // Return Type not specified = dynamic
  214.   if (_int < 0) {
  215.     throw new FormatException();
  216.   }
  217.  
  218.   return _int;
  219. }
Add Comment
Please, Sign In to add comment