-
Notifications
You must be signed in to change notification settings - Fork 1
/
33-Class_1.py
65 lines (52 loc) · 1.96 KB
/
33-Class_1.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
'''
author : Jaydatt Patel
class in python :
protected variable: The members of a class are not available to access from the exterior of the class. Accessibility is permitted within the class or its subclasses only. In Python, a data member is protected by prefixing it with a single underscore "_".
syntax:
_variableName
_functionName()
private variable: Private members are secured, but these cannot be accessed even by the super or parent class's subclasses, and a double underscore "__" is used as a prefix to inform the interpreter that a data member is private.
syntax:
__variableName
__functionName()
'''
class Point:
# creating variables
var1 = 1234
var2 = 'Python'
# constructor with default paramere arguments
def __init__(self,x = 999,y = 999): # self is currunt pointing object, like 'this' in java or c++
self.x = x # if var not defined in self class then it will define new variable
self.y = y
# function in class
def show(self):
return {'X' : self.x,'Y' : self.y}
# creating an object using different constructor
obj1 = Point(25,41)
obj2 = Point(9,13)
obj3 = Point(50)
obj4 = Point(y=15,x=16)
obj5 = Point()
print("obj1 is obj2 : ", obj1 is obj2)
print('------------1-------------')
print('obj1 :', obj1.show())
print('obj2 :', obj2.show())
print('obj3 :', obj3.show())
print('obj4 :', obj4.show())
print('obj5 :', obj5.show())
print('------------2-------------')
Point.get = 55 # create variable in class at anywere
obj1.z = 10 # create variable in only for object at anywere
print('Point.get : ',Point.get)
print('obj1.get : ',obj1.get)
print('obj1.z : ',obj1.z)
print('------------3-------------')
obj1.var1 = 356 # this willl
print("Point.var1 : ",Point.var1)
print("obj1.var1 : ",obj1.var1)
print("obj2.var1 : ",obj2.var1)
print('------------4-------------')
Point.var1 = 985
print("Point.var1 : ",Point.var1)
print("obj1.var1 : ",obj1.var1)
print("obj2.var1 : ",obj2.var1)