Class yapilari

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)


<__main__.cartesian object at 0x000001CD9B210C50>
True
False
1.25
4 5
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-68d9c1d256e5> in <module>()
     26 print(cartesian2.slope())
     27 print(cartesian2.getX(), cartesian2.y)
---> 28 print(cartesian.__x, cartesian.y)

AttributeError: type object 'cartesian' has no attribute '__x'

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)


(7, 9)

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)


(7, 9)

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)


True
__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

Inheritance

Bazen classlardaki bazi degiskenleri o kadar cok ayni seye atariz ki bir subclass olusturmak daha mantikli olmaya baslar. Gelin once bir Pet classi olusturup daha sonra Dog subclassi olusturarak turu kopege esitleyelim:


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)


Karabas: Kopecik

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)


Karabas: Kopek

In [ ]: