Tags

, , , , , , , ,

Learn Dart the Hard Way

The first and foremost task of constructors is the construction of objects. Whenever we try to create an object and write this line:
var fatherBear = Bear();
We actually try to arrange a spot in the memory for that object. The real work begins when we connect that spot with class properties and methods.
Using ‘constructor’ we can do that job more efficiently. Not only that, Dart allows to create more than one ‘constructor’, which is a great advantage.
Let us write our ‘Bear’ class in a new way of using constructor:

//code 3.10
class Bear {
int numberOfFish;
int hourOfSleep;
int weightGain;
Bear(this.numberOfFish, this.hourOfSleep);
int eatFish(int numberOfFish) => numberOfFish;
int sleepAfterEatingFish(int hourOfSleep) => hourOfSleep;
int weightGaining(int weightGain) => weightGain = numberOfFish * hourOfSleep;
}
main(List arguments){
var fatherBear = Bear(6, 10);
fatherBear.weightGain = fatherBear.numberOfFish * fatherBear.hourOfSleep;
print("Father bear eats ${fatherBear.eatFish(fatherBear.numberOfFish)} fishes. And he sleeps for ${fatherBear.sleepAfterEatingFish(fatherBear.hourOfSleep)} hours.");
print("Father bear has gained ${fatherBear.weightGaining(fatherBear.weightGain)} pounds of weight.");
}

Creating ‘constructor’ is extremely easy. Watch this line:

Bear(this.numberOfFish, this.hourOfSleep);

The same class name works as a function or method and we have passed two arguments through that method. Once we get those values, we would calculate the third variable. Writing constructor this way is known as “Syntactic Sugar”. In the later section of the book we will know more about the constructor.
Now it gets easier to pass the two values while creating the object. We could have done the same by creating constructor this way, which is more traditional:

//code 3.11
class Bear {
int numberOfFish;
int hourOfSleep;
int weightGain;
Bear(int numOfFish, int hourOfSleep){
this.numberOfFish = numOfFish;
this.hourOfSleep = hourOfSleep;
}
//Bear(this.numberOfFish, this.hourOfSleep);
int eatFish(int numberOfFish) => numberOfFish;
int sleepAfterEatingFish(int hourOfSleep) => hourOfSleep;
int weightGaining(int weightGain) => weightGain = numberOfFish * hourOfSleep;
}
main(List arguments){
var fatherBear = Bear(6, 10);
fatherBear.weightGain = fatherBear.numberOfFish * fatherBear.hourOfSleep;
print("Father bear eats ${fatherBear.eatFish(fatherBear.numberOfFish)} fishes. And he sleeps for ${fatherBear.sleepAfterEatingFish(fatherBear.hourOfSleep)} hours.");
print("Father bear has gained ${fatherBear.weightGaining(fatherBear.weightGain)} pounds of weight.");
}

In both cases, the output is same as before:

//output of code 3.11
Father bear eats 6 fishes. And he sleeps for 10 hours.
Father bear has gained 60 pounds of weight.

In the above code, you can even get the object’s type very easily. We can change the type of value quite easily. Watch the main() function again:

//code 3.12
main(List arguments){
var fatherBear = Bear(6, 10);
fatherBear.weightGain = fatherBear.numberOfFish * fatherBear.hourOfSleep;
print("Father bear eats ${fatherBear.eatFish(fatherBear.numberOfFish)} fishes. And he sleeps for ${fatherBear.sleepAfterEatingFish(fatherBear.hourOfSleep)} hours.");
print("Father bear has gained ${fatherBear.weightGaining(fatherBear.weightGain)} pounds of weight.");
print("The type of the object : ${fatherBear.weightGain.runtimeType}");
String weightGained = fatherBear.weightGain.toString();
print("The type of the same object has changed to : ${weightGained.runtimeType}");
}

And here is the output:

//output of code 3.12
Father bear eats 6 fishes. And he sleeps for 10 hours.
Father bear has gained 60 pounds of weight.
The type of the object : int
The type of the same object has changed to : String

Advertisements