dart constructor class code example
Example 1: constructor in dart
void main(){
SelfDrivingCar myLamb = SelfDrivingCar('Floride');
myLamb.drive();
}
class Car {
int numberofwheels = 4;
void drive() {
print('this is a car');
}
}
class SelfDrivingCar extends Car {
String destination='k';
SelfDrivingCar(String userDestination){
destination = userDestination;
}
@override
void drive() {
super.drive();
print('sterring wheel to $destination');
}
}
Example 2: dart constructor
void main() {
Human jenny = Human(height1 :15);
print(jenny.height);
Human jerry = Human(height1: 20);
print(jerry.height);
}
class Human {
double height = 0;
Human({height1 = 0}) {
this.height = height1;
}
}