Python入门题032:类和函数级静态变量

题目:

实现类和函数级的静态变量。

视频教程:

Python入门题032:类和函数级静态变量

代码1:

class People:
    Type = '人'

    def __init__(self, name):
        self.name = name

    def show(self):
        print(f'{self.name} 是 {self.Type}')


class Teacher(People):
    pass


class Student(People):
    pass


teacher_li = Teacher('李老师')
student_ming = Student('小明')
student_hong = Student('小红')

print('------ 1')
teacher_li.show()
student_ming.show()
student_hong.show()

print('------ 2')
People.Type = '僵尸'

teacher_li.show()
student_ming.show()
student_hong.show()

print('------ 3')
People.Type = '人'
Student.Type = '僵尸'

teacher_li.show()
student_ming.show()
student_hong.show()

print('------ 4')
People.Type = '人'

teacher_li.show()
student_ming.show()
student_hong.show()

代码2:

def ticktock():
    if not hasattr(ticktock, 'counter'):
        ticktock.counter = 0
    print(f'欢迎光临,这是你第 {ticktock.counter} 次来了!')
    ticktock.counter += 1


ticktock()
ticktock()
ticktock()



展开阅读全文

页面更新:2024-03-24

标签:变量   静态   函数   入门   僵尸   视频教程   题目   代码   欢迎光临   科技

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top