====== 7. Objekty ======
===== Komplexní čísla =====
# definice tridy - popis entity
class Complex:
# kontruktor
def __init__ (self, r, i):
self.re = r
self.im = i
self.print_format = "slozky"
def print (self):
print("{} {}".format(self.re, self.im))
def __str__ (self):
if self.print_format == "slozky":
return "{0}{1:+}i".format(round(self.re,2), round(self.im, 2))
else:
return "A*e^i(modul)"
def add (self, other):
self.re = self.re + other.re
self.im = self.im + other.im
def __add__ (self, other):
return Complex(self.re + other.re, self.im + other.im)
def modul (self):
""" vypocet modulu """
def angle (self):
""" vypocet uhlu """
# instance
a = Complex(2.2, 3.3)
b = Complex(-4.1, 0.2)
# a.add(b)
c = a + b
print(id(a))
print(type(a))
print("a = {}".format(a))
print("b = {}".format(b))
print("c = {}".format(c))
===== Knihovna =====
class Book:
def __init__ (self, title, author, year):
self.title = title
self.author = author
self.year = year
self.sorting_order = 0 # 0 - title, 1 - surname, 2 - year
def __str__ (self):
return "title: {}\nauthor: {}\nyear: {}".format(self.title, self.author, self.year)
def __lt__ (self, other):
if self.sorting_order == 0:
return self.title < other.title
elif self.sorting_order == 1:
a = self.author.split()
b = other.author.split()
return a[1] < b[1]
elif self.sorting_order == 2:
return self.year < other.year
knihovna = []
kniha_a = Book ("Honzikova cesta", "Bohumil Riha", 1954)
kniha_b = Book ("Cuk a Gek", "Arkadij Gajdar", 1935);
knihovna.append (kniha_a)
knihovna.append (kniha_b)
knihovna.append (Book("Robinson Crusoe", "Daniel Defoe", 1800))
knihovna = sorted(knihovna)
for i in range(len(knihovna)):
print(knihovna[i])
print("------------")