-
Notifications
You must be signed in to change notification settings - Fork 387
/
20_constructors.dart
53 lines (37 loc) · 1.24 KB
/
20_constructors.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Objectives
// 1. Default Constructor
// 2. Parameterized Constructor
// 3. Named Constructor
// 4. Constant Constructor
void main() {
var student1 = Student(23, "Peter"); // One Object, student1 is reference variable
print("${student1.id} and ${student1.name}");
student1.study();
student1.sleep();
var student2 = Student(45, "Sam"); // One Object, student2 is reference variable
print("${student2.id} and ${student2.name}");
student2.study();
student2.sleep();
var student3 = Student.myCustomConstructor(); // One object, student3 is a reference variable
student3.id = 54;
student3.name = "Rahul";
print("${student3.id} and ${student3.name}");
var student4 = Student.myAnotherNamedConstructor(87, "Paul");
print("${student4.id} and ${student4.name}");
}
// Define states (properties) and behavior of a Student
class Student {
int id = -1;
String name;
Student(this.id, this.name); // Parameterised Constructor
Student.myCustomConstructor() { // Named Constructor
print("This is my custom constructor");
}
Student.myAnotherNamedConstructor(this.id, this.name); // Named Constructor
void study() {
print("${this.name} is now studying");
}
void sleep() {
print("${this.name} is now sleeping");
}
}