Accessing Class Members
Any class member ie. class variable or method
(function) can be accessed by using object with a dot ( . ) operator.
Object_name . class_member
class Sample:
#class variables
x, y = 10, 20
S=Sample( ) # class instantiation
print("Value of x = ",
S.x)
print("Value of y = ",
S.y)
print("Value of x and y =
", S.x+S.y)
Output :
Value of x = 10
Value of y = 20
Value of x and y = 30
In the above code, the name of the class is
Sample. Inside the class, we have assigned the variables x and y with initial
value 10 and 20 respectively. These two variables are called as class variables
or member variables of the class. In class instantiation process, we have
created an object S to access the
members of the class. The first two print statements simply print the value of
class variable x and y and the last print statement add the
two values and print the result.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.