-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoverriding.py
More file actions
50 lines (33 loc) · 1.08 KB
/
Copy pathoverriding.py
File metadata and controls
50 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
class Animal: #parent
def toString():
print("animal")
class Monkey(Animal): #child
def toString():
print("monkey")
a1 = Animal()
a1.toString()
m1 = Monkey()
m1.toString() #hem parent classta hem de child classta toString methodu var.
#monkey classı burada override methodunu çağırır.
#javadaki gibi @override yazmaya gerek yoktur.
class Rectangle(): #parent
def __init__(self,length,breadth):
self.length = length
self.breadth = breadth
def getArea(self):
print(self.length*self.breadth," is area of rectangle")
class Square(Rectangle): #child
def __init__(self,side):
self.side = side
Rectangle.__init__(self,side,side)
def getArea(self):
print(self.side*self.side," is area of square")
s = Square(4)
r = Rectangle(2,4)
s.getArea() #square classı içindeki getArea methodu çalışır.
r.getArea() #rectangle classı içindeki getArea methodu çalışır.