Created
April 2, 2019 05:40
-
-
Save anantakrroy/5d69c3b5a37cb8dbf146d954b2fcde0c to your computer and use it in GitHub Desktop.
Fundamentals of Dart
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
// INHERITANCE | |
// void main() { | |
// Lizard liz = Lizard(); | |
// // Cat kitty = Cat(); | |
// // kitty.test(); | |
// liz.test(); // Testing in Lizard, Animal, and Reptile | |
// liz.crawl(); // Now we can call crawl() from Reptile class | |
// } | |
// class Animal { | |
// void test() => print("Testing in Animal"); | |
// } | |
// class Reptile { | |
// bool canCrawl = true; | |
// void crawl() => print("Crawl"); | |
// void test() => print("Testing in Reptile"); | |
// } | |
// class Cat extends Animal { | |
// bool hasTwoPointyEars = true; | |
// void meow() => print("Meow!!"); | |
// @override | |
// void test() { | |
// print("Testing in Cat"); | |
// super.test(); | |
// } | |
// } | |
// class Lizard extends Animal with Reptile { | |
// bool hasTail = true; | |
// @override | |
// void test() { | |
// print("Testing in Lizard"); | |
// super.test(); | |
// } | |
// } | |
// INTERFACES | |
// void main(){ | |
// Manager srd = new Manager(); | |
// srd.printSomething(); | |
// } | |
// class Worker { | |
// String name = ""; | |
// void printSomething() => print("Worker Printing"); | |
// } | |
// class Manager implements Worker { | |
// String name = "Sharad"; | |
// void printSomething() => print("Manager Printing"); | |
// } | |
// ABSTRACTION | |
// void main() { | |
// var rect = Rectangle(); | |
// rect.draw(); | |
// } | |
// abstract class Shape { | |
// String x; // Can also define instance variable | |
// void draw(); // abstract method | |
// // We can also define normal functions as well | |
// } | |
// // Whenever we extend a abstract class, then its mendatory to override that class's abstract methods | |
// class Rectangle extends Shape { | |
// String x = "yo!"; | |
// void draw() => print("Overrided!!"); | |
// } | |
// GENERICS | |
// void main(){ | |
// add<int>(1, 2); // prints 3 | |
// add<double>(1.0, 2.0); // prints 3.0 | |
// add<String>("Sharad", "Ghimire"); // prints SharadGhimire | |
// //addNumbers<String>("a", "b"); // Gives error but still work?? | |
// } | |
// void add<T>(T a, T b) { // T is shorthand for type | |
// print(a + b); // But, for String, the operator + isn't defined for the class 'Object' | |
// } | |
// void addNumbers<T extends num>(T a, T b){ // num includes int and doubles so it does not work for String | |
// print(a +b); | |
// } | |
// void main() { | |
// var studentName = "John Doe"; | |
// print(studentName?.toUpperCase()); | |
// } | |
// String printRoll() { | |
// //int rollNo = 52; | |
// print("The roll number alloted to you is : $rollNo.toString()"); | |
// } | |
/// EXCEPTION HANDLING | |
// CASE 1 - when the exception is known | |
// void main() { | |
// // int result = 12~/0; | |
// // print("The answer is: $result"); | |
// // on IntegerDivisionByZero { | |
// // print("Cannot divide by zero"); | |
// // } | |
// print(" "); | |
// // CASE 2: When the specific exception is not known | |
// try { | |
// int result = 12~/0; | |
// print(" The result is $result"); | |
// } catch(e) { | |
// print("The exception raised is $e"); | |
// } | |
// print(""); | |
// // CASE 3: When the stack trace needs to be printed | |
// try { | |
// int result = 24~/0; | |
// print("Result: $result"); | |
// } catch(e,s) { | |
// print("Exception raised is $e and the stack trace is \n $s"); | |
// } | |
// print(""); | |
// // CASE 4 : Whether or not an exception is thrown execute the FINALLY clause | |
// try { | |
// int result = 24~/12; | |
// print("Result: $result"); | |
// } catch(e) { | |
// print("Exception raised is $e"); | |
// } finally { | |
// print("FINALLY clause executed"); | |
// } | |
// print(" "); | |
// // CASE 5: Custom exception handling | |
// try { | |
// amountDeposit(-200); | |
// } catch(e){ | |
// print(e.errorMessage()); | |
// } | |
// } | |
// class DepositException implements Exception { | |
// String errorMessage() { | |
// return "Cannot enter amount less than 0"; | |
// } | |
// } | |
// void amountDeposit(int amount) { | |
// // int amount = -100; | |
// if(amount < 0) { | |
// throw DepositException(); | |
// } | |
// } | |
// class Person { | |
// DateTime birthday; | |
// String name; | |
// final int DAYS = 365; | |
// Person(this.name, {this.birthday}); | |
// void set age(double years) { | |
// var d = new DateTime.now(); | |
// var dur = new Duration(days: (DAYS*years).toInt()); | |
// d = d.subtract(dur); | |
// birthday = d; | |
// } | |
// double get age { | |
// var d = new DateTime.now(); | |
// return d.difference(birthday).inDays / DAYS; | |
// } | |
// double myage() { | |
// var d = new DateTime.now(); | |
// return d.difference(birthday).inDays / DAYS; | |
// } | |
// } | |
// main() { | |
// var p = new Person("Foo"); | |
// print(p.name); | |
// p.age = 18.0; | |
// print(p.age); | |
// print(p.myage()); // () required because it is not a real getter | |
// var o = new Person("Bar", birthday: DateTime.parse("19900212")); | |
// print(o.name); | |
// print(o.birthday); | |
// print("${o.age} is the age in years"); | |
// } | |
// HIGHER ORDER FUNCTIONS | |
// accepts a function as a parameter OR returns a function | |
void main() { | |
// calling the higher order function | |
Function addNum = (int a, int b) => print(a + b); | |
helloMessage("Hello", addNum); | |
var squaredNumber = squareNum(); | |
print(squaredNumber(10)); | |
} | |
// Case 1 : higher order function, with function as a parameter | |
void helloMessage(String message, Function myFunc) { | |
// print("FUNCTION"); | |
print("$message,"); | |
myFunc(3, 7); | |
} | |
// Case 2 : higher order function which returns a function | |
Function squareNum() { | |
Function square = (int number) => number * number; | |
return square; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment