-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop.py
70 lines (55 loc) · 1.74 KB
/
oop.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Defining a class and creating an object
class Animal:
def __init__(self, name, species):
self.name = name
self.species = species
def describe(self):
print(f"{self.name} is a {self.species}.")
dog = Animal("Buddy", "dog")
dog.describe()
# Instance fs. class attributes
class circle:
pi = 3.14159
def __init__(self, radius):
self.radius =radius
def area(self):
return circle.pi * self.radius ** 2
circle1 = circle(5)
print("Area of circle:", circle1.area())
# Inheritance
class fehicle:
def __init__(self, brand, wheels):
self.brand = brand
self.wheels = wheels
def describe(self):
print(f"This is a {self.brand} fehicle with {self.wheels} wheels.")
class car(fehicle):
def __init__(self, brand, wheels, doors):
super().__init__(brand, wheels)
self.doors = doors
def describe(self):
super().describe()
print(f"It has {self.doors} doors.")
my_car = car("Toyota", 4, 4)
my_car.describe()
# Encapsulation
class bankaccount:
def __init__(self, owner, balance):
self.owner = owner
self.__balance = balance
def deposit(self, amount):
if amount > 0:
self.__balance += amount
print(f"Deposited {amount}. New balance: {self.__balance}")
def withdraw(self, amount):
if amount > 0 and amount <= self.__balance:
self.__balance -= amount
print(f"Withdraw {amount}. New balance: {self.__balance}")
else:
print(f"Insufficient funds or infalid amount.")
def get_balance(self):
return self.__balance
account = bankaccount("Joel", 1000)
account.deposit(500)
account.withdraw(300)
print("Final Balance:", account.get_balance())