forked from smartherd/DartTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Getters and Setters along with Private Instance variable
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |