python开发基础(五)面向对象--类属性和实例属性
2021/8/6 1:36:06
本文主要是介绍python开发基础(五)面向对象--类属性和实例属性,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# 类属性的增删改查 import time class Chinesepeople: country = 'China' person_type = '人' def __init__(self, name): self.name = name def play_ball(self, ball): print('%s正在玩%s' % (self.name, ball)) def eat_foot(self, foot): print('%s正在吃%s' % (self.name, foot)) # 查看类属性 print(Chinesepeople.country) # 修改类属性 Chinesepeople.country = 'CHINA' print(Chinesepeople.country) # 增加类属性 Chinesepeople.time_now = time.asctime() # 删除类属性 print(Chinesepeople.__dict__) del Chinesepeople.person_type print(Chinesepeople.__dict__) person1 = Chinesepeople('jack') person1.play_ball(ball='ball') print(person1.time_now)
# 实例属性的增删改查 class Chinese: def __init__(self, name): self.name = name def play_ball(self, ball): print('%s正在完%s' % (self.name, ball)) person1 = Chinese('alex') # 查看 print(person1.name) # 增加 person1.age = 18 print(person1.age) # 修改 person1.age = 20 print(person1.age) print(person1.__dict__) # 删除 del person1.age print(person1.__dict__)
这篇关于python开发基础(五)面向对象--类属性和实例属性的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战
- 2024-09-18Python量化交易:入门指南与实践
- 2024-09-18Python量化交易:入门指南与实战技巧
- 2024-09-14Python人工智能项目实战:从零开始的实践指南
- 2024-09-14探索Python人工智能资料:初学者的指南
- 2024-09-14Python人工智能资料:初学者的全面指南
- 2024-09-13Matplotlib入门:轻松绘制Python数据可视化图表
- 2024-09-13Python人工智能:初学者的入门指南
- 2024-09-13Python人工智能:轻松入门与实践