-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathClass_Excercise.py
More file actions
155 lines (111 loc) · 3.85 KB
/
Class_Excercise.py
File metadata and controls
155 lines (111 loc) · 3.85 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Write a Python program to create a Vehicle class with max_speed and mileage instance attributes.
class Vehicle:
def __init__(self, max_speed, mileage):
self.max_speed = max_speed
self.mileage = mileage
modelX = Vehicle(240, 18)
print("Maximum Speed of Vehicle is :", modelX.max_speed, "\n" "Maximum Mileage given by Vehicle is :", modelX.mileage)
# Create a Vehicle class without any variables and methods
class Vehicle:
pass
# Define a class that can add and subtract two numbers.
class add_sub:
def __init__(self, x, y):
self.x = x
self.y = y
# define 'add' method
def add(self):
return self.x + self.y
# define 'subtract' method
def subtract(self):
return self.x - self.y
if __name__ == '__main__':
x = 10
y = 6
# create an instance
opp = add_sub(x, y)
# call add method
print("The Function return the difference between", x, "and ", y, 'is: ', opp.add())
# print(opp.add())
# call subtract method
print("The Function return the difference between", x, "and ", y, 'is: ', opp.subtract())
# Define class and instance attributes
class Cylinder:
# class attribute
pi = 3.14
def __init__(self, radius, height):
# instance variables
self.radius = radius
self.height = height
if __name__ == '__main__':
c1 = Cylinder(4, 20)
c2 = Cylinder(10, 50)
print('pi for c1:', c1.pi, "and c2:", c2.pi)
print('Radius for c1:', c1.radius, "and c2:", c2.radius)
print('Height for c1:', c1.height, "and c2:", c2.height)
print('Pi for both c1 and c2 is:', Cylinder.pi)
# Define class and instance methods
class Cylinder:
# class attribute
pi = 3.14
def __init__(self, radius, height):
# instance variables
self.radius = radius
self.height = height
# instance method
def volume(self):
return Cylinder.pi * self.radius ** 2 * self.height
# class method
@classmethod
def description(cls):
return 'This is a Cylinder class that computes the volume using Pi=', cls.pi
if __name__ == '__main__':
c1 = Cylinder(40, 2) # create an instance/object
print('Volume of Cylinder:', c1.volume()) # access instance method
print(Cylinder.description()) # access class method via class
print(c1.description()) # access class method via instance
# Render a class’s attributes and methods protected.
class Article:
def __init__(self, title, page_count):
# initialize protected attributes
self._title = title
self._page_count = page_count
# define protected method
def _show(self):
# access protected attributes inside class
print("Article Title: ", self._title)
print("Page Count: ", self._page_count)
class Author(Article):
def __init__(self, name, title, page_count):
Article.__init__(self, title, page_count)
self.name = name
def display(self):
print("Author Name: ", self.name)
# access Article's protected method
self._show()
print("------------------ \n")
author = Author("Eyong Kevin", "Python Classes and Objects", 3000)
author.display()
# access protected data
print(author._title)
# Render a class’s attributes and methods private.
class Language:
# private class attribute
__country = "Cameroon"
def __init__(self, name):
# initialize private instance attribute
self.__name = name
# private method
def __show(self):
# access private attributes
print("Country: ", Language.__country)
print("Name :", self.__name)
# public method
def display(self):
# access private method within class
self.__show()
lang = Language("English")
lang.display()
# access private variable and method
lang._Language__show()
print("Access Private Name: ", lang._Language__name)