-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontructors.dart
More file actions
63 lines (46 loc) · 1.37 KB
/
contructors.dart
File metadata and controls
63 lines (46 loc) · 1.37 KB
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
54
55
56
57
58
59
60
61
62
63
void main() {
var student = Student(1, "Lmao");
/*new keyword is optional is dart
DEFAULT CONSTRUCTOR*/
print("${student.id} and ${student.name}");
student.study();
student.sleep();
var s2 = Student(2, "lol"); //PARAMETERISED CONSTRUCTOR
print("\n${s2.id} and ${s2.name}");
s2.sleep();
s2.study();
var s3 = Student.myCustomConstructor();
s3.id = 3;
s3.name = "haha";
print("${s3.id} and ${s3.name}");
var s4 = Student.myAnotherCustomConstructor(3, "kkkkk");
print("\n${s4.id} and ${s4.name}");
}
class Student {
int id;
String name; //Instance or Field var, default value is null
/**
* within the same class we don't have both Default and Parameterized constructor
*/
// Student() {
// print("This is DEFAULT CONSTRUCTOR");
// }
Student(this.id, this.name); //PARAMETERISED CONSTRUCTOR
Student.myCustomConstructor() {
//NAMED CONSTRUCTOR
print("This is named constructor");
}
Student.myAnotherCustomConstructor(this.id, this.name); //NAMED CONSTRUCTOR
/**
* within one class you can have multiple named constructor but
* can't have DEFAULT and PARAMETERISED constructor at the same time
*/
void study() {
int marks; //Local var
print("${this.name} is now studying");
//this => refers to the current instance of the object
}
void sleep() {
print("${this.name} is now sleeping");
}
}