forked from smartherd/DartTutorial
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21_getters_setters.dart
More file actions
27 lines (19 loc) · 807 Bytes
/
21_getters_setters.dart
File metadata and controls
27 lines (19 loc) · 807 Bytes
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
// Objectives
// 1. Default Getter and Setter
// 2. Custom Getter and Setter
// 3. Private Instance Variable
void main() {
var student = Student();
student.name = "Peter"; // Calling default Setter to set value
print(student.name); // Calling default Getter to get value
student.percentage = 438.0; // Calling Custom Setter to set value
print(student.percentage); // Calling Custom Getter to get value
}
class Student {
String name; // Instance Variable with default Getter and Setter
double _percent; // Private Instance Variable for its own library
// Instance variable with Custom Setter
void set percentage(double marksSecured) => _percent = (marksSecured / 500) * 100;
// Instance variable with Custom Getter
double get percentage => _percent;
}