Created
January 16, 2013 10:33
-
-
Save saumya/4546231 to your computer and use it in GitHub Desktop.
A basic dart file to show most of the basics of the dartlang.
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() { | |
//print("Hello, World!"); | |
num a=24; | |
printNumber(a); | |
var b=45;//declaring without a type declaration, but you can see assigning a string to this will throw compile time error | |
printNumber(b); | |
Joe j=new Joe(); | |
//print(j.age);//can not access private var | |
print('j is of ${j.userAge}');//getter | |
j.userAge=30;//setter | |
print('j is of ${j.userAge}');//getter | |
print('j is from city ${j.fromCity}'); | |
Person p1=new Person.inCity('Pune'); | |
print('p1 is from ${p1.fromCity}'); | |
Person p2=new Person(); | |
print('p2 is from ${p2.fromCity}'); | |
} | |
void printNumber(num aNumber){ | |
print('The number is $aNumber'); | |
} | |
class Person { | |
int _age=20;//private variable | |
String fromCity='Unknown';//public variable | |
//default constructor | |
Person(){ | |
print('Person : Default constructor'); | |
} | |
//named constructor | |
Person.inCity(String nameOfCity){ | |
this.fromCity=nameOfCity; | |
} | |
//getter | |
int get userAge{ | |
print('Person : getter : '); | |
return this._age; | |
} | |
void set userAge(int n){ | |
print('Person : setter : '); | |
this._age=n; | |
} | |
} | |
//extending the class | |
class Joe extends Person{ | |
Joe(){ | |
print('Joe : Default constructor'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment