
TI/Skrypty z zajęć/k6: Różnice pomiędzy wersjami
Z Brain-wiki
|  (Utworzono nową stronę "<source lang="python"> a </source>") | |||
| Linia 1: | Linia 1: | ||
| <source lang="python"> | <source lang="python"> | ||
| − | + | # -*- coding: utf-8 -*- | |
| + | """ | ||
| + | Created on Wed May 27 11:11:31 2020 | ||
| + | |||
| + | @author: Tomek | ||
| + | """ | ||
| + | |||
| + | class CiagArytmetyczny(): | ||
| + |     def __init__(self, a1, r): | ||
| + |         self.__a1 = float(a1) | ||
| + |         self.__r = float(r) | ||
| + | |||
| + |     def __get_a1__(self): | ||
| + |         return self.__a1 | ||
| + | |||
| + |     def __get_r__(self): | ||
| + |         return self.__r | ||
| + | |||
| + |     def __set_a1__(self, a1): | ||
| + |         self.__a1 = float(a1) | ||
| + | |||
| + |     def __set_r__(self, r): | ||
| + |         self.__r = float(r) | ||
| + | |||
| + |     a1 = property(__get_a1__,__set_a1__) | ||
| + |     r = property(__get_r__,__set_r__) | ||
| + | |||
| + |     def __getitem__(self,n): | ||
| + |         if isinstance(n,int): | ||
| + |             if (n > 0): | ||
| + |                 return self.__a1 + (n-1)*self.__r | ||
| + | |||
| + |     def __add__(self, other): | ||
| + |         return CiagArytmetyczny(self.a1 + other.a1, self.r + other.r) | ||
| + | |||
| + |     def __str__(self): | ||
| + |         return 'CiagArytmetyczny a1 = '+str(self.__a1)+' r = '+str(self.__r) | ||
| + | |||
| + |     def __repr__(self): | ||
| + |         return str(self) | ||
| + | |||
| + | z = CiagArytmetyczny(1,1) | ||
| + | x = CiagArytmetyczny(100,1) | ||
| + | |||
| + | class CiagArytmetycznyZeSlownikiem(CiagArytmetyczny): | ||
| + | |||
| + |     def __init__(self, a1, r, sl = None): | ||
| + |         #self(ale potraktuj mnie jako obiekt klasy CiagAtrytmetyczny).__init__(a1,r) | ||
| + |         super().__init__(a1,r) | ||
| + |         self.__sl = sl | ||
| + |         if self.__sl==None: | ||
| + |             self.__sl = {} | ||
| + | |||
| + | |||
| + |     def __setitem__(self, key, value): | ||
| + |         self.__sl[key] = value | ||
| + | |||
| + |     @property | ||
| + |     def sl(self): | ||
| + |         return self.__sl.copy() | ||
| + | |||
| + |     def __str__(self): | ||
| + |         return 'CiagArytmetycznyZeSlownikiem a1 = '+str(self.a1)+' r = '+str(self.r)+' oraz sl = '+ str(self.sl) | ||
| + | |||
| + |     def __add__(self, other): | ||
| + |         return CiagArytmetycznyZeSlownikiem(self.a1 + other.a1, self.r + other.r) | ||
| + | |||
| + | |||
| + | y = CiagArytmetycznyZeSlownikiem(50,1) | ||
| + | |||
| + | class CiagArytmetycznyR1(CiagArytmetyczny): | ||
| + |     def __init__(self, a1): | ||
| + |         super().__init__(a1,1) | ||
| + | |||
| + | |||
| </source> | </source> | ||
Aktualna wersja na dzień 19:51, 27 maj 2020
# -*- coding: utf-8 -*-
"""
Created on Wed May 27 11:11:31 2020
@author: Tomek
"""
class CiagArytmetyczny():
    def __init__(self, a1, r):
        self.__a1 = float(a1)
        self.__r = float(r)
    def __get_a1__(self):
        return self.__a1
    def __get_r__(self):
        return self.__r
    
    def __set_a1__(self, a1):
        self.__a1 = float(a1)
    def __set_r__(self, r):
        self.__r = float(r)
    
    a1 = property(__get_a1__,__set_a1__)
    r = property(__get_r__,__set_r__)
    
    def __getitem__(self,n):
        if isinstance(n,int):
            if (n > 0):
                return self.__a1 + (n-1)*self.__r
    
    def __add__(self, other):
        return CiagArytmetyczny(self.a1 + other.a1, self.r + other.r)
    def __str__(self):
        return 'CiagArytmetyczny a1 = '+str(self.__a1)+' r = '+str(self.__r)
    
    def __repr__(self):
        return str(self)
    
z = CiagArytmetyczny(1,1)
x = CiagArytmetyczny(100,1)
class CiagArytmetycznyZeSlownikiem(CiagArytmetyczny):
    
    def __init__(self, a1, r, sl = None):
        #self(ale potraktuj mnie jako obiekt klasy CiagAtrytmetyczny).__init__(a1,r)
        super().__init__(a1,r)
        self.__sl = sl
        if self.__sl==None:
            self.__sl = {}
        
    
    def __setitem__(self, key, value):
        self.__sl[key] = value
        
    @property
    def sl(self):
        return self.__sl.copy()
    def __str__(self):
        return 'CiagArytmetycznyZeSlownikiem a1 = '+str(self.a1)+' r = '+str(self.r)+' oraz sl = '+ str(self.sl)
    
    def __add__(self, other):
        return CiagArytmetycznyZeSlownikiem(self.a1 + other.a1, self.r + other.r)
y = CiagArytmetycznyZeSlownikiem(50,1)
class CiagArytmetycznyR1(CiagArytmetyczny):
    def __init__(self, a1):
        super().__init__(a1,1)

