Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
kurs_alx_pcz
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
kurs_alx_pcz
Commits
538fa786
Commit
538fa786
authored
Dec 01, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dziedziczenie - override
parent
640c3c42
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
2 deletions
+24
-2
klasy3.py
dzien7/klasy_wprowadzenie/klasy3.py
+24
-2
No files found.
dzien7/klasy_wprowadzenie/klasy3.py
View file @
538fa786
...
...
@@ -8,7 +8,7 @@ class Osoba:
return
f
'{self.imie} {self.nazwisko} ({self.wiek} lat)'
def
przedstaw_sie
(
self
):
print
(
f
'Nazywam się {self.imie} {self.nazwisko} i mam {self.wiek} lat'
)
print
(
f
'Nazywam się {self.imie} {self.nazwisko} i mam {self.wiek} lat
.
'
)
def
jest_pelnoletnia
(
self
):
return
self
.
wiek
>=
18
...
...
@@ -20,9 +20,25 @@ class Student(Osoba):
super
()
.
__init__
(
imie
,
nazwisko
,
wiek
)
self
.
rok
=
rok
self
.
kierunek
=
kierunek
self
.
oceny
=
[]
# Klasa podrzędna może:
# a) niczego nie zmieniać w odziedziczonej metodzie - np. nie zmieniamy "jest_pelnoletnia"
# b) dodać nowe metody - obsługa ocen
def
dodaj_ocene
(
self
,
ocena
):
self
.
oceny
.
append
(
ocena
)
def
srednia_ocen
(
self
):
return
4.5
import
statistics
if
not
self
.
oceny
:
return
None
return
statistics
.
mean
(
self
.
oceny
)
# c) nadpisać (override) metodę, czyli zmienić jej implementację na inną
def
przedstaw_sie
(
self
):
# możliwe jest: super().przedstaw_sie()
print
(
f
'Hej, tu {self.imie} {self.nazwisko}, studiuję {self.kierunek}.'
)
class
Sklep
:
...
...
@@ -58,8 +74,14 @@ print('dane studenta:', student.imie, student.nazwisko, student.kierunek)
# Tylko obiekty klasy Student posiadają dodatkową metodę srednia_ocen
#ERR print('średnia ocen osoby:', osoba.srednia_ocen())
student
.
dodaj_ocene
(
5
)
student
.
dodaj_ocene
(
4
)
student
.
dodaj_ocene
(
4
)
print
(
'średnia ocen studenta:'
,
student
.
srednia_ocen
())
print
()
osoba
.
przedstaw_sie
()
student
.
przedstaw_sie
()
print
(
'Przedstawimy studenta w taki sposób, jak przedstawia się osoba:'
)
Osoba
.
przedstaw_sie
(
student
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment