Skip to content

Commit 6ce61ff

Browse files
Aktualizacja
1 parent 45c7500 commit 6ce61ff

File tree

6 files changed

+38
-17
lines changed

6 files changed

+38
-17
lines changed

17_Wstep_do_obiektowosci/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 18. Wstęp do obiektowości
1+
# 17. Wstęp do obiektowości
22

33
---
44

17_Wstep_do_obiektowosci/animal_spirits.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ def play(self):
3737
return "Dling Dling"
3838

3939

40-
orchestra = [Guitar(), Guitar(), Drums()]
40+
class Kaczka:
41+
def play(self):
42+
return "Kwa kwa"
43+
44+
45+
orchestra = [Guitar(), Guitar(), Drums(), Kaczka()]
4146
for instrument in orchestra:
4247
print(instrument.play())
4348

@@ -76,8 +81,8 @@ def get_speed(self):
7681

7782

7883
class Swallow(ABC):
79-
def __init__(self):
80-
self.base_speed = 120
84+
def __init__(self, base_speed=120):
85+
self.base_speed = base_speed
8186
self.load_factor = 15
8287
self.number_of_coconuts = 3
8388

@@ -87,6 +92,7 @@ def get_speed(self):
8792

8893

8994
class EuropeanSwallow(Swallow):
95+
9096
def get_speed(self):
9197
return self.base_speed
9298

@@ -97,6 +103,7 @@ def get_speed(self):
97103

98104

99105
class NorwegianSwallow(Swallow):
106+
100107
def get_speed(self):
101108
return self.base_speed - self.load_factor * self.number_of_coconuts * self.number_of_coconuts
102109

17_Wstep_do_obiektowosci/food_items.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ def __repr__(self):
1717
with open('foods.csv') as csvfile:
1818
reader = csv.DictReader(csvfile)
1919
for row in reader:
20-
food_list.append(FoodItem(item_id=row['Food ID'], item_name=row['Food Item'], price=row['Price']))
20+
food_list.append(FoodItem(item_id=row['id'], item_name=row['name'], price=row['price']))
2121

2222
pprint(food_list)

17_Wstep_do_obiektowosci/hermetyzacja.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ def get_salary(self):
2222

2323

2424
john = Employee('John Doe', 'computer lab', 1000)
25+
john.get_salary()
26+
27+
2528
john.__salary # error
2629

27-
john.get_salary()
30+
2831

2932
john._Employee__salary
3033

@@ -36,17 +39,17 @@ def __init__(self, x, y):
3639
self.x = x
3740
self.y = y
3841

39-
42+
@property
4043
def r(self):
4144
return math.sqrt(self.x * self.x + self.y * self.y)
4245

4346

4447
p = Point(3.0, 4.0)
45-
p
48+
4649
print(p.r)
4750
p.r = 7 # error
48-
p.r()
49-
# Anatomia obiektu
51+
p.r() # error
5052

53+
# Anatomia obiektu
5154
print(p.__dict__)
5255
print(Point.__dict__)

17_Wstep_do_obiektowosci/oop_1.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ def f(self):
1111
o1 = MyClass(1)
1212
o2 = MyClass(2)
1313

14+
1415
o1
1516

1617
o1.i
1718
o2.i
1819
o1.n
1920
o2.n
20-
o1.f()
21+
o1.f() # -> f(o1)
2122
o2.f()
2223

23-
2424
class MyClass:
2525
i = 0
2626

17_Wstep_do_obiektowosci/special_methods.py

+16-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ def __gt__(self, other):
4040

4141
b > a # b.__gt__(a)
4242

43-
b
43+
b < a
44+
4445

4546

4647
class IntValue:
@@ -148,28 +149,38 @@ def __repr__(self):
148149

149150
#############################################################
150151

151-
values = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')
152-
suits = ('clubs', 'diamonds', 'hearts', 'spades')
153-
154152

155153
class Card(object):
154+
values = ('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A')
155+
suits = ('clubs', 'diamonds', 'hearts', 'spades')
156+
156157
def __init__(self, value='A', suit='spade'):
157158
self.value, self.suit = value, suit
158159
print(f"In init; value is {value}; suit is {suit}")
159160

160161
def __repr__(self):
161162
return "Card('%s','%s')" % (self.value, self.suit)
162163

164+
def __gt__(self, other):
165+
if self.suits.index(self.suit) > self.suits.index(other.suit):
166+
return True
167+
168+
if self.suits.index(self.suit) < self.suits.index(other.suit):
169+
return False
170+
171+
return self.values.index(self.value) > self.values.index(other.value)
172+
163173

164174

165175
krol_karo = Card('K', 'diamonds')
166176
as_karo = Card('A', 'diamonds')
167177
dycha_karo = Card('10', 'diamonds')
168178
karty_w_rece = [ as_karo, krol_karo, dycha_karo]
169179

170-
karty_w_rece
180+
print(karty_w_rece)
171181

172182
karty_w_rece.sort()
183+
print(karty_w_rece)
173184

174185
def moc_karty(karta):
175186
return len(values)*suits.index(karta.suit) + values.index(karta.value)

0 commit comments

Comments
 (0)