TI/Ciąg Arytmetyczny
Z Brain-wiki
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 10:36:23 2019
@author: tgub
"""
class CiagA():
#licznik obiektów klasy CiagA
licznik=0
@staticmethod
def kwadratowa(x):
return x*x
@classmethod
def ilenas(cls):
return cls.licznik
def __init__(self,a0,r):
self.__a0=a0
self.__r=r
CiagA.licznik+=1
def __del__(self):
print('AAAAA umieram.......')
CiagA.licznik-=1
@property
def a0(self):
return self.__a0
@property
def r(self):
return self.__r
def __getitem__(self,key):
if key<0: raise KeyError
return self.a0+key*self.r
def __add__(self,other):
return CiagA(self.a0+other.a0,self.r+other.r)
def __str__(self):
return 'Ciag Arytmetyczny o a0='+str(self.a0)+' r='+str(self.r)
def __repr__(self):
return self.__str__()
class CiagAE(CiagA):
#licznik obiektów klasy CiagAE
licznik=0
def __init__(self,a0,r=None):
if isinstance(a0,CiagA):
r=a0.r
a0=a0.a0
super().__init__(a0,r)
CiagAE.licznik+=1
self.__slownik={}
def __del__(self):
print('AEAEAEAEAE umieram.......')
CiagAE.licznik-=1
super().__del__()
def __getitem__(self,key):
try:
return self.__slownik[key]
except KeyError:
return super().__getitem__(key)
def __setitem__(self,key,value):
self.__slownik[key]=value
def __str__(self):
return super().__str__()+' oraz zmiany opisane slownikiem '+str(self.__slownik)
def __add__(self,other):
temp=CiagAE(self.a0+other.a0,self.r+other.r)
for key in self.__slownik.keys():
temp[key]=self[key]+other[key]
try:
for key in other.__slownik.keys():
temp[key]=self[key]+other[key]
except AttributeError:
pass
return temp
y=CiagAE(4,20)
x=CiagA(1,2)
y[1]=1000