Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 3885ae1

Browse files
committed
Issue #44 - updated code snippet
Signed-off-by: Serhii Horodilov <sgorodil@gmail.com>
1 parent 4a06269 commit 3885ae1

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

src/oop/intro.txt

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -263,43 +263,33 @@ leading underscore stripped.
263263
.. code-block:: python
264264

265265
class Employee:
266-
"""Employee class implementation"""
266+
"""Employee superclass example"""
267267

268-
def __init__(self, salary: float, hours: int = None) -> None:
269-
"""Initialize an employee instance"""
270-
271-
self.__salary = salary
272-
self._hours = hours
268+
first_name: str = ""
269+
last_name: str = ""
273270

274-
def report_hours(self, hours: int) -> None:
275-
"""Report working hours"""
271+
_rate: int = 0
272+
__tax: float = 0.18
276273

277-
# some validation stuff goes here
278-
self._hours += hours
274+
def _get_amount(self, hours: int) -> int:
275+
return self._rate * hours
279276

280-
def change_salary(salary: float) -> None:
281-
"""Change salary size"""
277+
def _get_tax(self, amount: int) -> int:
278+
return int(round(self.__tax * amount, 0))
282279

283-
self.__salary = salary
280+
def get_balance(self, hours: int) -> int:
281+
amount = self._get_amount(hours)
282+
tax = self._get_tax(amount)
284283

285-
def get_payment(self) -> float:
286-
"""Return a payment size"""
284+
return amount - tax
287285

288-
return round(self.__salary * self._hours, 2)
286+
def set_rate(self, rate: int) -> None:
287+
self._rate = rate
289288

290-
With the implementation above there are **public** attributes, but you can
291-
still access data outside the class, but you definitely shouldn't.
292289

293-
.. code-block:: python
290+
class Employee10PercentTax(Employee):
291+
_Employee__tax = 0.10
294292

295-
employee = Employee(100_500)
296-
# you're able to make changes to protected member, but this code smells
297-
# use dedicated method `report_hours` instead
298-
employee._hours = 100
299-
# accessing Employee.__salary attribute will raise an exception
300-
# the interpreter will add _className before each private member
301-
# but you know - `change_salary` method is that you need
302-
employee._Employee__salary
303293

304294
Few Words about Inheritance
305295
===========================

0 commit comments

Comments
 (0)