Classlar belli veri tipleri alan objeler olusturup onlari olusturdugumuz metodlarla manipule etmeyi saglayan yapilardir. Ayrica operator overloadinge imkan tanirlar. Takim halinde calisirken implementasyonu classlarin ardina gizleyip diger developerlara basit bir arayuz sunmamizi saglarlar.
In [20]:
class cartesian:
__x = 0
y = 0
def __init__(self, x, y):
self.__x = x
self.y = y
def slope(self):
return self.y / self.__x
def getX(self):
return self.__x
def __add__(self, other):
self.__x += other.getX()
self.y += other.y
def __eq__(self, other):
if self.__x == other.getX() and self.y == other.y:
return True
else:
return False
cartesian1= cartesian(3, 4)
cartesian2 = cartesian(3, 4)
print(cartesian1)
print(cartesian1 == cartesian2)
cartesian2 = cartesian(4, 5)
print(cartesian2 == cartesian1)
print(cartesian2.slope())
print(cartesian2.getX(), cartesian2.y)
print(cartesian.__x, cartesian.y)
In [19]:
class cartesian:
__x = 0
y = 0
def __init__(self, x, y):
self.__x = x
self.y = y
def slope(self):
return self.y / self.__x
def getX(self):
return self.__x
def __add__(self, other):
return cartesian(self.__x + other.getX(), self.y + other.y)
def __eq__(self, other):
if self.__x == other.getX() and self.y == other.y:
return True
else:
return False
def __str__(self):
return str((self.__x, self.y))
cartesian1= cartesian(3, 4)
cartesian2 = cartesian(4, 5)
cartesian3 = cartesian1 + cartesian2
print(cartesian3)
In [21]:
class cartesian:
__x = 0
__y = 0
def __init__(self, x, y):
self.__x = x
self.__y = y
def slope(self):
return self.__y / self.__x
def getX(self):
return self.__x
def getY(self):
return self.__y
def __add__(self, other):
return cartesian(self.__x + other.getX(), self.__y + other.getY())
def __eq__(self, other):
if self.__x == other.getX() and self.__y == other.getY():
return True
else:
return False
def __str__(self):
return str((self.__x, self.__y))
cartesian1= cartesian(3, 4)
cartesian2 = cartesian(4, 5)
cartesian3 = cartesian1 + cartesian2
print(cartesian3)
In [25]:
class cartesian:
__x = 0
__y = 0
def __init__(self, x, y):
self.__x = x
self.__y = y
def slope(self):
return self.__y / self.__x
def getX(self):
return self.__x
def getY(self):
return self.__y
def __add__(self, other):
return cartesian(self.__x + other.getX(), self.__y + other.getY())
def __eq__(self, other):
if self.__x == other.getX() and self.__y == other.getY():
return True
else:
return False
def __str__(self):
return str((self.__x, self.__y))
def __lt__(self, other):
if self.__x * self.__x + self.__y * self.__y < other.getX() * other.getX() + other.getY() * other.getY():
return True
else:
return False
cartesian1= cartesian(3, 4)
cartesian2 = cartesian(4, 4)
print(cartesian1 < cartesian2)
__eq__ = equals to (esittir)
__lt__ = less than (kucuktur)
__le__ = less than or equals to (kucuk esittir)
__ne__ = not equals to (esit degildir)
__gt__ = greater than (buyuktur)
__ge__ = greater than or equals to (buyuk esittir)
Operator overloading yapabilecegimiz fonksiyonlar : https://docs.python.org/3/reference/datamodel.html#special-method-names
In [29]:
class Pet(object):
__name = ''
__species = ''
def __init__(self, name, species):
self.__name = name
self.__species = species
def getName(self):
return self.__name
def __str__(self):
return str(self.__name + ': ' + self.__species)
myDog = Pet('Karabas', 'Kopecik')
print(myDog)
In [33]:
class Pet(object):
__name = ''
__species = ''
def __init__(self, name, species):
self.__name = name
self.__species = species
def getName(self):
return self.__name
def __str__(self):
return str(self.__name + ': ' + self.__species)
class Dog(Pet):
def __init__(self, name):
Pet.__init__(self, name, 'Kopek')
myDog = Dog('Karabas')
print(myDog)
In [ ]: