Created
August 12, 2020 09:35
-
-
Save MbuguaCaleb/939212d58c46d4575c0c0ac3b5eec420 to your computer and use it in GitHub Desktop.
Dart Primer tutorial by Net Ninja on the basics of dart
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() { | |
//Instantiating a class | |
User userOne=User('caleb', 24); | |
//calling an attribute/property of the class | |
print(userOne.username); | |
print(userOne.age); | |
User userTwo = User('mercy',23); | |
print(userTwo.username); | |
print(userTwo.age); | |
//Invoking a function from the class | |
userOne.login(); | |
/*Inheritance*/ | |
SuperUser userThree = SuperUser("Humphrey",58); | |
print(userThree.username); | |
/*Invoking the extra method from the inherited class*/ | |
userThree.publish(); | |
/*has access to methods from the parent class*/ | |
userThree.login(); | |
} | |
class User { | |
//class attributes | |
String username; | |
int age ; | |
//constructor function | |
//Should have the same name as the class | |
//It is a special type of function therefore | |
//does not take void | |
User(String username , int age ){ | |
this.username =username; | |
this.age=age; | |
} | |
void login (){ | |
print('user logged in'); | |
} | |
} | |
/*Inheritance*/ | |
/*The SuperUser class wants to inherit from the User | |
but have some added functionlities which a normal | |
user should not have | |
*/ | |
class SuperUser extends User{ | |
//super calls the constructor from the class it extends from | |
//and passes those values in. | |
/* We as well inherit the functions of the previous | |
class plus it now has an added function/method. | |
*/ | |
SuperUser(String username, int age ):super(username,age); | |
void publish (){ | |
print("published update!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment