Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b462cc

Browse files
update
1 parent 38ed8bf commit 1b462cc

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

‎03_Python_Revision/12_Revise_again_all_python_concepts/OOP_revision.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,68 @@ def __init__(self,name,breed,age):
157157
def __sound(self):
158158
print("Woof")
159159
def __eat(self,meal):
160-
print(f"{self.__name} is eating {meal}")
160+
print(f"{self.__name} is eating {meal}")
161+
162+
c1 = Dog("Bella","germen","4")
163+
c1.__name = "Bella" # This will raise an AttributeError
164+
# c1.__sound() # This will raise an AttributeError
165+
# c1.__eat("Biscuit") # This will raise an AttributeError
166+
167+
#how to access the private attributes
168+
c1._Dog__name = "Bella"
169+
c1._Dog__sound()
170+
c1._Dog__eat("Biscuit")
171+
print(c1._Dog__name)
172+
print(c1._Dog__breed)
173+
print(c1._Dog__age) # This will print 0 because we didn't pass
174+
175+
176+
# POLYMORPHISM
177+
class Animal:
178+
def sound(self):
179+
pass
180+
class Dog(Animal):
181+
def sound(self):
182+
return "Woof"
183+
class Cat(Animal):
184+
def sound(self):
185+
return "Meow"
186+
187+
188+
c2 = Cat()
189+
c3 = Dog()
190+
print(c2.sound()) # Output: Meow
191+
print(c3.sound()) # Output: Woof
192+
193+
#ABSTRACTION
194+
from abc import ABC, abstractmethod
195+
class Shape:
196+
@abstractmethod
197+
def area(self):
198+
pass
199+
@abstractmethod
200+
def perimeter(self):
201+
pass
202+
class Circle:
203+
def __init__(self,r):
204+
self.radius = r
205+
def area(self):
206+
return 3.14 * (self.radius ** 2)
207+
def perimeter(self):
208+
return 2 * 3.14 * self.radius
209+
class Rectangle:
210+
def __init__(self,l,w):
211+
self.length = l
212+
self.width = w
213+
def area(self):
214+
return self.length * self.width
215+
def perimeter(self):
216+
return 2 * (self.length + self.width)
217+
218+
c = Circle(5)
219+
r = Rectangle(4,5)
220+
print(c.area())
221+
print(c.perimeter())
222+
print(r.area())
223+
print(r.perimeter())
224+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /