2019-01-03
阅读量:
726
python怎么实现示例变量
=实例变量(或属性)
在Python中,实例变量是其值在构造函数或方法中使用self分配的变量。
类变量是其值在类中赋值的变量。
# Python program to show that the variables with a value
# assigned in class declaration, are class variables and
# variables inside methods and constructors are instance
# variables.
# Class for Computer Science Student
class CSStudent:
# Class Variable
stream = 'cse'
# The init method or constructor
def __init__(self, roll):
# Instance Variable
self.roll = roll
# Objects of CSStudent class
a = CSStudent(101)
b = CSStudent(102)
print(a.stream) # prints "cse"
print(b.stream) # prints "cse"
print(a.roll) # prints 101
# Class variables can be accessed using class
# name also
print(CSStudent.stream) # prints "cse"






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论