-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
149 lines (107 loc) · 5.06 KB
/
Program.cs
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
namespace Prezentacja;
using Structures;
using Classes;
using Interfaces;
using Singleton;
class Program
{
static void Main(string[] args)
{
Structures();
Classes();
Interfaces();
Singletons();
ValueSemantics();
ReferenceSemantics();
}
static void Structures() {
Console.WriteLine("Structures");
// Inicjalizator obiektu może zostać uruchomiony bezpośrednio
// Bez wcześniejszego utworzenia instancji struktury
Point myPoint0 = new Point { X = 0, Y = 0 };
myPoint0.Print();
// Konstruktor oparty o 2 parametry
Point myPoint1 = new Point(0, 1);
myPoint1.Print();
// Konstruktor oparty o 1 parametr
Point myPoint2 = new Point(2);
myPoint2.Print();
// Konstruktor bez parametrów
Point myPoint3 = new Point();
myPoint3.Print();
// Konstruktor oparty o 1 parametr typu string
Point myPoint4 = new Point("4");
myPoint4.Print();
// Konstruktor oparty o 2 parametry typu string
Point myPoint5 = new Point("5", "6");
myPoint5.Print();
}
static void Classes() {
Console.WriteLine("Classes");
// Samochody
Car myCar = new Car("Ford", "Mustang", 2000, "Black");
myCar.Print();
Trabant myTrabant = new Trabant("601", 1969, "White");
myTrabant.Print();
Ferrari myFerrari = new Ferrari(model: "F40", year: 1987, turbo: true, abs: false);
myFerrari.Print();
}
static void Interfaces() {
Console.WriteLine("Interfaces");
// Sprawdzamy czy możemy wykonać metodę przyjmującą interfejs jako parametr
Point myPoint = new Point(1, 2);
InterfaceTester.TestInterface(myPoint);
Car myCar = new Car("Ford", "Mustang", 2000, "Black");
InterfaceTester.TestInterface(myCar);
ElectricTrabant electricTrabant = new ElectricTrabant("nT", 2009, "White", 100);
float distance = electricTrabant.GetDistance();
Console.WriteLine($"Distance: {distance}");
}
static void Singletons() {
Console.WriteLine("Singleton");
SingletonExample singleton = SingletonExample.GetInstance();
Console.WriteLine($"Counter: {singleton.GetCount()}");
for (int i = 0; i < 5; i++) {
singleton.Increment();
}
Console.WriteLine($"Counter: {singleton.GetCount()}");
SingletonExample newReference = SingletonExample.GetInstance();
Console.WriteLine($"New reference Counter: {newReference.GetCount()}");
newReference.Reset();
Console.WriteLine($"New reference Counter after reset: {newReference.GetCount()}");
Console.WriteLine($"Old reference Counter after reset: {singleton.GetCount()}");
}
static void ValueSemantics() {
Console.WriteLine("Value Semantics");
Point structPoint = new Point(20, 20);
Console.WriteLine($"structPoint, X: {structPoint.X}, Y: {structPoint.Y}");
Point copyOfStructPoint = structPoint;
Console.WriteLine($"copyOfStructPoint, X: {copyOfStructPoint.X}, Y: {copyOfStructPoint.Y}");
structPoint.X = 30;
Console.WriteLine($"structPoint, X: {structPoint.X}, Y: {structPoint.Y}");
Console.WriteLine($"copyOfStructPoint, X: {copyOfStructPoint.X}, Y: {copyOfStructPoint.Y}");
}
static void ReferenceSemantics() {
Console.WriteLine("Reference Semantics");
ClassPoint classPoint = new ClassPoint(20, 20);
Console.WriteLine($"classPoint, X: {classPoint.X}, Y: {classPoint.Y}");
ClassPoint copyOfClassPoint = classPoint;
Console.WriteLine($"copyOfClassPoint, X: {copyOfClassPoint.X}, Y: {copyOfClassPoint.Y}");
classPoint.X = 30;
Console.WriteLine($"classPoint, X: {classPoint.X}, Y: {classPoint.Y}");
Console.WriteLine($"copyOfClassPoint, X: {copyOfClassPoint.X}, Y: {copyOfClassPoint.Y}");
// Czy da się przekazywać referencje do struktur?
Point structPoint = new Point(20, 20);
Console.WriteLine($"structPoint, X: {structPoint.X}, Y: {structPoint.Y}");
ref Point referenceOfStructPoint = ref structPoint;
Console.WriteLine($"referenceOfStructPoint, X: {referenceOfStructPoint.X}, Y: {referenceOfStructPoint.Y}");
structPoint.X = 30;
Console.WriteLine($"structPoint, X: {structPoint.X}, Y: {structPoint.Y}");
Console.WriteLine($"referenceOfStructPoint, X: {referenceOfStructPoint.X}, Y: {referenceOfStructPoint.Y}");
// Struktura może być przechowywana na stercie (Heap)?
object boxedPoint = structPoint;
Console.WriteLine($"boxedPoint, X: {((Point)boxedPoint).X}, Y: {((Point)boxedPoint).Y}");
object boxedPointReference = boxedPoint;
Console.WriteLine($"boxedPointReference, X: {((Point)boxedPointReference).X}, Y: {((Point)boxedPointReference).Y}");
}
}