Dart enumerations,classes and objects
Enumerations
Named list of related items
enum PersonProperties { firstName, lastName, age }
void test() {
print(PersonProperties.firstName);
}
Switch is the prefreed way of doing enumerations
Classes
class Person {
void run() {
print('Running');
}
void breathe() {
print('Brething');
}
}
void test() {
//we have to first create instance of class to use it or we will get error
final person = Person();
person.run();
}
Objects
final foo = Person();
Constructor
class Person {
final String name;
Person(this.name);
}
void test() {
final foo = Person('him');
print(foo.name);
}
Methods
class Person {
final String name;
Person(this.name);
void printName() {
print('I will now priny name of person');
print(name);
}
}
void test() {
final foo = Person('him gogoi');
foo.printName();
}
Inheritance & Sub classing
class LivingThing {
void breathe() {
print('Liivng thing is breathing');
}
void move() {
print('I am moving');
}
}
class Cat extends LivingThing{}
void test() {
final fluffers = Cat();
fluffers.move();
fluffers.breathe();
}
Abstract Class
Abstract class can't be intitiated. Other classes can use its features but it can't initiate itself
Factory Constructor
It is almost similar like prototype of Javascript
class Cat {
final String name;
Cat(this.name);
factory Cat.fluffBall() {
return Cat('Fluff Ball');
}
}
void test() {
final fluffers = Cat.fluffBall();
print(fluffers.name);
}
Comments
Post a Comment