-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClasses.dart
89 lines (64 loc) · 1.78 KB
/
Classes.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Class 1
// ignore_for_file: unused_field
class Person {
String? name = "No";
String? s2 = "Yo";
// Constructor
Person() {
print("$s2 i am a constructor");
}
// Method
void print_name([String? name]) {
print("$name I am a print method .");
}
}
class Base {
Base() {
print("I am a Constructor of Base ");
}
Base.witha(int a) {
print("I am a Constructor of Base with $a");
}
// private Variables
int _height =
4; // an _ use with the variable name to assign it as a private variable
int _width =
5; // private variables only access and modifies in same class and modifies using setters and getters
// setters and Getters
// Setter to acces or read only value of height using setter
int get height => _height;
// Getter to modify the value of height
set setheight(int h) {
_height = h;
}
}
// Inheritance Touch
class Derived extends Base {
Derived() {
print("I am a Constructor of Derived");
}
Derived.witha(int a) {
print("I am a Constructor of Derived with $a");
}
// Static variables and methods
// Static variables can be accessed by the name of class without creating instance/object of the class
int new_age = 18;
static int age = 18;
static int my_age() {
return age;
}
}
void main() {
//Person person1 = Person(); // Object of Person1
//person1.print_name("Hello");
Base obj1 = Base();
//Base obj2 = Base.witha(4);
//Derived obj3 = Derived(); // first call base constructor then derive const
//Derived obj4 = Derived();
print(obj1.height);
obj1.setheight = 10; // update private variable value
print(obj1.height);
// print(Derived.new_age); // error can be access by using static variable
print(Derived.age); //Variable
print(Derived.my_age()); // Method
}