编程语言
首页 > 编程语言> > python3:类变量和类的成员变量

python3:类变量和类的成员变量

作者:互联网

 

 

python3:类变量和类的成员变量:

 

 

 

 

[root@rockylinux tmp]# cat python_test 
#!/usr/bin/python3



class test:

    # 类变量
    name="SunWukong"
    age=300

    def set_name(self, nm, ag):
        # 类的成员变量
        self.name = nm
        self.age = ag


    def msg(self):
        print("name=" + self.name)
        print("age=" + str(self.age))



tmp = test()
tmp.set_name("RuLai", 999)
tmp.msg()


# “成员变量”的调用方法:对象名称.成员变量名称
# tmp.name  tmp.age
print("tmp.name = " + tmp.name)
print("tmp.age = " + str(tmp.age))


# “类变量”的调用方法:类的名称.类变量的名称
# test.name  test.age
print("test.name = " + test.name)
print("test.age = " + str(test.age))
[root@rockylinux tmp]# 
[root@rockylinux tmp]# 
[root@rockylinux tmp]# 
[root@rockylinux tmp]# 
[root@rockylinux tmp]# ./python_test 
name=RuLai
age=999
tmp.name = RuLai
tmp.age = 999
test.name = SunWukong
test.age = 300
[root@rockylinux tmp]# 
[root@rockylinux tmp]# 
[root@rockylinux tmp]# 
[root@rockylinux tmp]# 
[root@rockylinux tmp]# 

 

标签:tmp,变量,age,rockylinux,test,root,成员,python3,name
来源: https://www.cnblogs.com/lnlidawei/p/16290964.html