mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-09-03 03:33:15 +02:00
Classes
This commit is contained in:
@@ -509,59 +509,58 @@ filter(lambda x: x > 5, [3, 4, 5, 6, 7])
|
|||||||
## 5. Třídy
|
## 5. Třídy
|
||||||
####################################################
|
####################################################
|
||||||
|
|
||||||
# We subclass from object to get a class.
|
# Třída Clovek je potomkem (dědí od) třídy object
|
||||||
class Human(object):
|
class Clovek(object):
|
||||||
|
|
||||||
# A class attribute. It is shared by all instances of this class
|
# Atribut třídy - je sdílený všemi instancemi
|
||||||
species = "H. sapiens"
|
druh = "H. sapiens"
|
||||||
|
|
||||||
# Basic initializer, this is called when this class is instantiated.
|
# Toto je kostruktor. Je volán, když vytváříme instanci třídy. Dvě
|
||||||
# Note that the double leading and trailing underscores denote objects
|
# podtržítka na začátku a na konci značí, že se jedná o atribut nebo
|
||||||
# or attributes that are used by python but that live in user-controlled
|
# objekt využívaný Pythonem ke speciálním účelům, ale můžete sami
|
||||||
# namespaces. Methods(or objects or attributes) like: __init__, __str__,
|
# definovat jeho chování. Metody jako __init__, __str__, __repr__
|
||||||
# __repr__ etc. are called magic methods (or sometimes called dunder methods)
|
# a další se nazývají "magické metody". Nikdy nepoužívejte toto
|
||||||
# You should not invent such names on your own.
|
# speciální pojmenování pro běžné metody.
|
||||||
def __init__(self, name):
|
def __init__(self, jmeno):
|
||||||
# Assign the argument to the instance's name attribute
|
# Přiřazení parametru do atributu instance jmeno
|
||||||
self.name = name
|
self.jmeno = jmeno
|
||||||
|
|
||||||
# An instance method. All methods take "self" as the first argument
|
# Metoda instance - všechny metody instance mají "self" jako první parametr
|
||||||
def say(self, msg):
|
def rekni(self, hlaska):
|
||||||
return "{name}: {message}".format(name=self.name, message=msg)
|
return "{jmeno}: {hlaska}".format(jmeno=self.jmeno, hlaska=hlaska)
|
||||||
|
|
||||||
# A class method is shared among all instances
|
# Metoda třídy - sdílená všemi instancemi
|
||||||
# They are called with the calling class as the first argument
|
# Dostává jako první parametr třídu, na které je volána
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_species(cls):
|
def vrat_druh(cls):
|
||||||
return cls.species
|
return cls.druh
|
||||||
|
|
||||||
# A static method is called without a class or instance reference
|
# Statická metoda je volána bez reference na třídu nebo instanci
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def grunt():
|
def odkaslej_si():
|
||||||
return "*grunt*"
|
return "*ehm*"
|
||||||
|
|
||||||
|
|
||||||
# Instantiate a class
|
# Vytvoření instance
|
||||||
i = Human(name="Ian")
|
d = Clovek(jmeno="David")
|
||||||
print(i.say("hi")) # prints out "Ian: hi"
|
a = Clovek("Adéla")
|
||||||
|
print(d.rekni("ahoj")) # Vypíše: "David: ahoj"
|
||||||
|
print(a.rekni("nazdar")) # Vypíše: "Adéla: nazdar"
|
||||||
|
|
||||||
j = Human("Joel")
|
# Volání třídní metody
|
||||||
print(j.say("hello")) # prints out "Joel: hello"
|
d.vrat_druh() # => "H. sapiens"
|
||||||
|
|
||||||
# Call our class method
|
# Změna atributu třídy
|
||||||
i.get_species() # => "H. sapiens"
|
Clovek.druh = "H. neanderthalensis"
|
||||||
|
d.vrat_druh() # => "H. neanderthalensis"
|
||||||
|
a.vrat_druh() # => "H. neanderthalensis"
|
||||||
|
|
||||||
# Change the shared attribute
|
# Volání statické metody
|
||||||
Human.species = "H. neanderthalensis"
|
Clovek.odkaslej_si() # => "*ehm*"
|
||||||
i.get_species() # => "H. neanderthalensis"
|
|
||||||
j.get_species() # => "H. neanderthalensis"
|
|
||||||
|
|
||||||
# Call the static method
|
|
||||||
Human.grunt() # => "*grunt*"
|
|
||||||
|
|
||||||
|
|
||||||
####################################################
|
####################################################
|
||||||
## 6. Modules
|
## 6. Moduly
|
||||||
####################################################
|
####################################################
|
||||||
|
|
||||||
# You can import modules
|
# You can import modules
|
||||||
|
Reference in New Issue
Block a user