Kilka prostych przykładów z programowanie objektowe w Python

240420202112

Koty

W 1]:
class  kolor_kota ( obiekt ):  
    def  __init__ ( self ,  imię ,  kolor ):  
        self . imię  =  imię  
        self . kolor  =  kolor
    
W 2]:
nk1  =  kolor_kota ( 'Kremówka' ,  'ruda' )  
nk2  =  kolor_kota ( 'Helen' ,  'biało-szary' )
W [3]:
print ( 'imię nk1:' , nk1 . imię )  
print ( 'kolor nk1:' , nk1 . kolor )  
print ( 'imię nk2:' ,  nk2 .  imię )  
print ( 'kolor nk2:' ,  nk2 . kolor )
imię nk1: Kremówka
kolor nk1: ruda
imię nk2:  Helen
kolor nk2: biało-szary

Dane kotów

In [4]:
class Dane_kotów(object): 
    def __init__(self, nazwa, kolor, rok, miejsce_urodzenia):
        self.nazwa = nazwa 
        self.kolor = kolor 
        self.rok = rok 
        self.miejsce_urodzenia = miejsce_urodzenia 
In [5]:
k1 = Dane_kotów('Helen', 'biało-czarny',2014,'Pruszków') 
print(k1.rok)
2014

Psy

In [6]:
class Psy(object):
    def __init__(self, imię, kolor):
             self.imię = imię
             self.kolor = kolor   
In [7]:
p1 = Psy('Daks', 'rudy')      
p2 = Psy('Foker', 'biały')
print(p1.imię)
print(p2.kolor)
Daks
biały

Traktory

In [8]:
class Traktory(object):
    def __init__(self, model, kolor, producent, udźwig):
        self.kolor = kolor 
        self.producent = producent 
        self.udźwig = udźwig 
        self.model = model
     
    def start(self): 
        print("Jedzie")
    
    def stop(self): 
        print("Staje")
        
    def przyspieszenie(self): 
        print("przyspieszenie...Przyśpieszenie działa")
    
    def skrzynia_biegów(self, Typ_skrzyni): 
        print("Praca_biegów")
        
        
Traktor1 = Traktory("Ursus C-152","Ursus", 'Czerwony', '487kNT' )    
In [9]:
print('stop:  ',Traktor1.stop()) #<-- funkja stop działa
print('udźwig:',Traktor1.udźwig) #<-- Atrybut __init__ działa
Staje
stop:   None
udźwig: 487kNT

Działka

In [10]:
class Dzialka: 
    
    def __init__(self, dlugosc, szerokosc, koszt_metra=0): 
        self.dlugosc = dlugosc 
        self.szerokosc = szerokosc 
        self.koszt_metra = koszt_metra
        
    def obwod(self): 
        return 2 * (self.dlugosc + self.szerokosc) 
        
    def powierzchnia(self): 
        return self.dlugosc * self.szerokosc
        
    def cena(self): 
        area = self.powierzchnia() 
        return (area * self.koszt_metra)/1000
        
        
In [11]:
Konstancin = Dzialka(430, 120, 185)

print("Obwód działki w konstancinie: print("Powierzchnia działki w konstancinie: print("Cena działki w konstancinie: tyś. zł 
Obwód działki w konstancinie: 1100
Powierzchnia działki w konstancinie: 51600 m^2
Cena działki w konstancinie: tyś. zł 9546.0

Wykres

In [12]:
class MyDataClass:
    
    def __init__(self, data, time):
        self.data = data
        self.time = time
    
    def plot(self): 
        plt.plot(self.time, self.data)

import matplotlib.pyplot as plt
In [13]:
data = [1,4,5,7,12,5]
time = [1,2,3,4,5,6]
kot = MyDataClass(data,time)
In [14]:
kot.plot()

Class z drukowaniem

In [22]:
class Rectangle:
    def __init__(self, length, breadth, unit_cost=0):
        self.length = length
        self.breadth = breadth
        self.unit_cost = unit_cost
        # print("Area of Rectangle:

    def __str__(self):
        return ("Area of Rectangle: def get_perimeter(self):
        return 2 * (self.length + self.breadth)

    def get_area(self):
        return self.length * self.breadth

    def calculate_cost(self):
        area = self.get_area()
        return area * self.unit_cost
In [24]:
print(Rectangle(123,45,13))
Area of Rectangle: 5535 cm^2
Cost of rectangular field: Rs. 71955 
In [25]:
class Rectangle:
    def __init__(self, length, breadth, unit_cost=0):
        self.length = length
        self.breadth = breadth
        self.unit_cost = unit_cost
        print(self)

    def __repr__(self):
        area_str = "Area of Rectangle: cost_str = "Cost of rectangular field: Rs. return area_str + "n" + cost_str

    def get_perimeter(self):
        return 2 * (self.length + self.breadth)

    def get_area(self):
        return self.length * self.breadth

    def calculate_cost(self):
        area = self.get_area()
        return area * self.unit_cost
In [26]:
r = Rectangle(160, 120, 2000)
Area of Rectangle: 19200 cm^2
Cost of rectangular field: Rs. 38400000 
In [ ]:
 
In [ ]: