- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_03.py
More file actions
Latest commit
43 lines (30 loc) · 883 Bytes
/
Copy pathbasic_03.py
File metadata and controls
43 lines (30 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
面向对象的程序设计:
继承
鸭子类型
"""
importtime
classHero():
'''
这是一个超级英雄
'''
def__init__(self,name='zhangsan'):
self.age=30
self.name=name
# 如何设置一个不能被外部访问的属性,如一个人的秘密
self.__age=100
# 如何设置带有标识位的属性,但可以被外部访问
self._age=90
defrun(self):
print('我开始跑了',self.name)
if__name__=='__main__':
lgy=Hero('林国样')
# 如何打印类的相关说明
print(lgy.__doc__)
# 如何查看一个类的所有属性和方法
print(dir(Hero))
# 如何打印一个类实例的所有方法
print(Hero.__dict__)
lgy.run()
# 如何按照格式,打印日期的结果
print(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()))