https://github.com/dart-lang/sdk
Dart Logo (Google): https://www.dartlang.org/
In this Dart Video tutorial, we take a look at Objects and Classes. We look at how you can create a class as a blue print or set of rules for how to define the behavior and data of an object. We look at class constructors, named constructors, getter functions, setter functions and fields. We also take a look at operator methods and how we can override other methods from parent classes in our own custom class data types. Through all of this, we also talk about basic inheritance.
When you build a class in any object oriented programming language, you need a way to instantiate the class into an object. Because the class defines how the object should hold data and how it should behave, the constructor is also paramount to this process. In Dart, we have the primary constructor which is generally used to populate the fields in the object and we can create named constructors which allow us to define further constants on the creation of our custom objects. These constructors are the main entry point for how the Class/Object interfaces with the rest of the language.
Complex object. This is important, because both of these values must contain some value to create a true Complex Number. The named constructors, Complex.real and Complex.imaginary both make use of the initializer operation. They use this operation to call the primary constructor with a specific set of rules. Complex.real populates the imaginary field with a zero and Complex.imaginary populates the real field with a zero. In this way, we can setup all types of complex numbers without having to always pass in two parameters.
As mentioned a few times in this tutorial series, all classes and objects in Dart a children of the Object type. This is an important abstraction because the Object type defines a set of properties and behaviors that all classes and objects must have. In this video, we look at the toString() method and the == operator specifically but the Object type also includes a few more important properties. These properties will automatically be applied to the child Objects unless the developer specifically overrides them with new behaviors.
In this image, an example is shown where we override both the toString() and == methods so that they fit the Complex Number abstraction better. The toString() method is called every time the object needs to be stringified. This includes when the object is printed to the console via the print function. In this case, we check to see if the imaginary field is negative. If its positive, we print the complex number with a real + imaginary i format. If it is negative, we print it with a real - imaginary.abs() i format. On the other hand, the == operator uses the real and imaginary fields to find out if the object is numerically equivalent to another object. If the object that we are comparing to is not aComplex object type, then it will always return false. If it is a Complex object type, then it will compare the real numbers and the imaginary numbers to one another.
The Source Code for this video may be found here: https://github.com/tensor-programming/dart_for_beginners/tree/tensor-programming-patch-2