-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path013_methods.dart
108 lines (85 loc) · 2.23 KB
/
013_methods.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* =============================================================================
# Author: Abdulkerim Akan - https://github.com/kerim47/
# Email: [email protected]
# FileName: 013_methods.dart
# Description: /
# Version: 0.0.1
# History:
============================================================================= */
import 'dart:math';
// Ornek (instance) metodlar
class Point {
final double x;
final double y;
Point(this.x, this.y);
double distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}
}
void foo1() {
var point = Point(0, 0);
print(point.distanceTo(Point(1, 1)));
}
// Operator aşırı yuklenmesi
class Vector {
final int x, y;
Vector(this.x, this.y);
Vector operator +(Vector v) => Vector(x + v.x, y + v.y);
Vector operator -(Vector v) => Vector(x - v.x, y - v.y);
@override
bool operator ==(Object other) =>
other is Vector && x == other.x && y == other.y;
@override
int get hashCode => Object.hash(x, y);
}
void foo2() {
final v = Vector(2, 3);
final w = Vector(2, 2);
assert(v + w == Vector(4, 5));
assert(v - w == Vector(0, 1));
}
// Getters and Setters
class Rectangle {
double left, top, width, height;
Rectangle(this.left, this.top, this.width, this.height);
double get right => left + width;
set right(double value) => left = value - width;
double get bottom => top + height;
set bottom(double value) => top = value - height;
}
void foo3() {
var rect = Rectangle(2, 4, 20, 15);
assert(rect.left == 2);
rect.right = 11;
assert(rect.left == -9);
}
// Soyut method
abstract class Doer {
void doSomething(); // Soyut metot.
void doSomethingElse() {
// Soyut metodu kullanarak işlemler yapabiliriz.
print("Doer is doing something else.");
}
}
class EffectiveDoer extends Doer {
void doSomething() {
print("EffectiveDoer is doing something.");
}
@override
void doSomethingElse() {
// Ebeveyn sınıfın metodu ezildi.
print("EffectiveDoer is doing something else.");
}
}
void foo4() {
var doer = EffectiveDoer();
doer.doSomething();
doer.doSomethingElse();
}
void foo5() {}
void foo6() {}
void main() {
foo4();
}