jrosendo
53af9f2ecb
|
2 years ago | |
---|---|---|
.. | ||
README.md | 2 years ago |
README.md
Person
Instructions
Create a class named Person
.
Its attributes:
- name -
string
- surname -
string
- cityOfOrigin -
string
- age -
int
- height -
int
Constructor:
- name -
required
- cityOfOrigin -
required
- age -
required
- height -
required
- surname -
optional
Object Oriented Programming
Dart supports object oriented programming which Flutter framework relies on. Classes have 2 main concepts: attributes and methods. Attributes are needed to store various data in the instance of Class and methods are functions, which can use Class attributes for various manipulations. To understand OOP and its difference from functional programming visit this website.
Here is an example of the Class in Dart:
class Point {
double x = 0; // attribute initialized to 0
double y = 0; // attribute initialized to 0
}
Point - name of the class, x, y - are attributes.
What if you want to initiate a Point with different x and y? To do that declare a Constructor - which is a function that specifies how to create object of a class given a set of parameters.
class Point {
double x = 0; // attribute
double y = 0; // attribute
// Constructor
Point(double x, double y) {
this.x = x; // initializing attributes
this.y = y;
}
}
In Dart we can also use Constructor declaration of type Constructor(param1, param2...); thus saving a few lines of code if all we want to do is to pass arguments to values.
class Point {
double x = 0;
double y = 0;
Point(this.x, this.y); // "this" is meant for current class.
}
Now let's instantiate two objects of class Point:
var p1 = Point(5, 4);
var p2 = Point(8, 3);
What if we would like to know what is the distance between these 2 points? We can do that by declaring a class function (or method of the class) and passing one of the objects as a parameter:
import 'dart:math';
class Point {
double x = 0; // field
double y = 0; // field
Point(double x, double y) { // constructor
this.x = x; // initializing field
this.y = y; // initializing field
}
// method
double distanceTo(Point end) {
return sqrt(pow((end.x - this.x), 2) + pow((end.y - this.y), 2));
}
}
var p1 = Point(5, 4);
var p2 = Point(8, 3);
print(p1.distanceTo(p2));
- Note: please see the documentation for examples.