Browse Source

docs(person): correct grammar

pull/1862/head
davhojt 2 years ago committed by Dav Hojt
parent
commit
7766a540fa
  1. 76
      subjects/mobile-dev/person/README.md

76
subjects/mobile-dev/person/README.md

@ -1,30 +1,32 @@
# Person ## Person
### Instructions ### Instructions
Create a class named `Person`. Create a `class` named `Person`.
Its attributes: Its attributes:
- name - `string` - `name`: `string`
- surname - `string` - `surname`: `string`
- cityOfOrigin - `string` - `cityOfOrigin`: `string`
- age - `int` - `age`: `int`
- height - `int` - `height`: `int`
Constructor: Constructor:
- name - `required` - `name`: `required`
- cityOfOrigin - `required` - `cityOfOrigin`: `required`
- age - `required` - `age`: `required`
- height - `required` - `height`: `required`
- surname - `optional` - `surname`: `optional`
### Object Oriented Programming ### 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](https://www.freecodecamp.org/news/object-oriented-programming-concepts-21bb035f7260/). Dart supports object oriented programming, and it features heavily in Flutter. Classes have 2 main concepts.
- Attributes: store data about the instance of a class.
- Methods: are func functions, which can use class attributes for various manipulations.
Here is an example of the Class in Dart: Here is an example of a class in Dart. `Point` is the name of the class, while `x` and `y` are attributes:
```dart ```dart
class Point { class Point {
@ -33,69 +35,65 @@ class Point {
} }
``` ```
**Point** - name of the class, **x, y** - are attributes. What if you want to initiate a `Point` with different `x` and `y`? We use constructors to solve this problem. A constructor is a function which specifies how to instantiate an instance of a class, from a set of given parameters.
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.
```dart ```dart
class Point { class Point {
double x = 0; // attribute // Attributes
double y = 0; // attribute double x = 0;
double y = 0;
// Constructor // Constructor
Point(double x, double y) { Point(double x, double y) {
this.x = x; // initializing attributes // Initializing attributes
this.y = y; this.y = y;
this.x = x;
} }
} }
``` ```
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. In Dart we can also use **constructor declaration** to save a few lines of code.
```dart ```dart
class Point { class Point {
double x = 0; double x;
double y = 0; double y;
Point(this.x, this.y); // "this" is meant for current class. Point(this.x, this.y);
} }
``` ```
Now let's instantiate two objects of class Point: Now let's instantiate two objects of class `Point`:
```dart ```dart
var p1 = Point(5, 4); var p1 = Point(5, 4);
var p2 = Point(8, 3); 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: Suppose we want to know the distance between these 2 points? We can do that by declaring a method, and passing one of the objects as a parameter:
```dart ```dart
import 'dart:math'; import 'dart:math';
class Point { class Point {
double x = 0; // field double x;
double y = 0; // field double y;
Point(double x, double y) { // constructor Point(this.x, this.y);
this.x = x; // initializing field
this.y = y; // initializing field // Method
}
// method
double distanceTo(Point end) { double distanceTo(Point end) {
return sqrt(pow((end.x - this.x), 2) + pow((end.y - this.y), 2)); return sqrt(pow((end.x - this.x), 2) + pow((end.y - this.y), 2));
} }
} }
```
```dart
void main() {
var p1 = Point(5, 4); var p1 = Point(5, 4);
var p2 = Point(8, 3); var p2 = Point(8, 3);
print(p1.distanceTo(p2)); print(p1.distanceTo(p2));
}
``` ```
- Note: please see the [documentation](https://dart.dev/guides/language/language-tour#classes) for examples. Check out the [class](https://dart.dev/guides/language/language-tour#classes) section of the dart language tutorial.

Loading…
Cancel
Save