python: object

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Peson.py
人类类
edit:
ide:
date:
 
"""
 
class Human(object):
    """
    人类类
    """
    #限制对象属性只能是SLOTS里面的变量名  对象绑定属性,不用先定义属性
    #__slots__ = ('sage','sname','sfrom')
    sage=0 #年龄
 
    sname="geovindu"  #名字
 
    sfrom="from china"
 
    def __init__(self,name, age,frm):
        """
 
        :param name:
        :param age:
        """
        self._name=name
        self._age=age
        self._frm=frm
 
 
 
    def sleeep(self):
        """
        睡觉
        :return: none
        """
        print(id(self))
        print(self._age,"岁的",self._name,'在睡觉')
 
 
 
    def eat(self):
        """
        吃饭
        :return: none
        """
        print(id(self))
        print(self._age,"岁的",self._name,"在吃饭")
 
    def instrudce(self):
        """
        自我介绍
        :return: none
        """
        print(f"{self._age}岁的{self._name}自我介绍:{self._frm}")
 
    def operateAllFunc(self):
        """
        处理所有方法
        :return: none
        """
        self.sleeep()
        self.eat()
        self.instrudce()

  

调用:

1
2
3
4
5
6
7
8
9
p=Peson.Human("geovindu",20,"我是中国人")
print(id(p)) #打印内存地址
p.sage=20
p.sname="涂聚文"
p.operateAllFunc()
print(p.sname)
p2=Peson.Human("涂聚文",30,"我是江西人")
print(id(p2))#打印内存地址
p2.operateAllFunc()

 

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
44
"""
HealthSpah.py
运动减肥类
edit:geovindu,Geovin Du
date:20230618
IDE:PyCharm 2023.1.2
 
"""
 
class Spa(object):
    """
    体重减肥计量类
    """
 
    def __init__(self,name,weight):
        """
 
        :param name: 姓名
        :param weight: 体重
        """
        self._name=name
        self._weight=weight
        self._num=0  #记录跑步次数
        self._enum=0 #记录吃饭次数
 
    def joggin(self):
        """
        跑步
        :return:none
        """
        self._weight-=0.5
        self._num+=1
        print(f"{self._name}运动第:{self._num}次,体重减至{self._weight} KG")
    def eat(self):
        """
        吃饭
        :return:none
        """
        self._weight+=1
        self._enum+=1
        print(f"{self._name}吃饭第{self._enum}次之后,体重为:{self._weight}KG")
 
    def __str__(self):
        return f"{self._name}运动第:{self._num}次,吃饭第{self._enum}次之后,体重为:{self._weight}KG"

  

调用:

1
2
3
4
5
6
h=HealthSpah.Spa('小明',70)
h.joggin()
h.joggin()
h.eat()
h.joggin()
print(h)

  

1
2
3
4
5
小明运动第:1次,体重减至69.5 KG
小明运动第:2次,体重减至69.0 KG
小明吃饭第1次之后,体重为:70.0KG
小明运动第:3次,体重减至69.5 KG
小明运动第:3次,体重减至69.5 KG,吃饭第1次之后,体重为:69.5KG

  

 

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
44
45
46
47
48
49
50
51
52
53
"""
HealthSpah.py
运动减肥类
edit:geovindu,Geovin Du
date:20230618
IDE:PyCharm 2023.1.2
构造函数(方法)--析构函数(方法)
构造方法:创建
析构方法:销毁前
"""
 
class Spa(object):
    """
    体重减肥计量类
    """
 
    def __init__(self,name,weight):
        """
        构造方法
        :param name: 姓名
        :param weight: 体重
        """
        self._name=name
        self._weight=weight
        self._num=0  #记录跑步次数
        self._enum=0 #记录吃饭次数
 
    def __del__(self):
        """
        析构方法
        :return:
        """
        print(f"{self._name}挂机了")
 
    def joggin(self):
        """
        跑步
        :return:none
        """
        self._weight-=0.5
        self._num+=1
        print(f"{self._name}运动第:{self._num}次,体重减至{self._weight} KG")
    def eat(self):
        """
        吃饭
        :return:none
        """
        self._weight+=1
        self._enum+=1
        print(f"{self._name}吃饭第{self._enum}次之后,体重为:{self._weight}KG")
 
    def __str__(self):
        return f"{self._name}运动第:{self._num}次,吃饭第{self._enum}次之后,体重为:{self._weight}KG"

  

1
2
3
4
5
del h #手动删除
 
# 不挂机
while True:
    pass

  

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
HealthSpah.py
运动减肥类
edit:geovindu,Geovin Du
date:20230618
IDE:PyCharm 2023.1.2
构造函数(方法)--析构函数(方法)
构造方法:创建
析构方法:销毁前
"""
 
class Spa(object):
    """
    体重减肥计量类
    """
 
 
    cout=0 #类属性,计算多少个类调用
    """
    类属性
    """
 
    def __init__(self,name,weight):
        """
        构造方法
        :param name: 姓名
        :param weight: 体重
        """
        self._name=name  #实例属性
        self._weight=weight #实例属性
        self._num=0  #记录跑步次数
        self._enum=0 #记录吃饭次数
        Spa.cout+=1
 
    def __del__(self):
        """
        析构方法
        :return:
        """
        print(f"{self._name}挂机了")
 
    def joggin(self):
        """
        跑步
        :return:none
        """
        self._weight-=0.5
        self._num+=1
        print(f"{self._name}运动第:{self._num}次,体重减至{self._weight} KG")
    def eat(self):
        """
        吃饭
        :return:none
        """
        self._weight+=1
        self._enum+=1
        print(f"{self._name}吃饭第{self._enum}次之后,体重为:{self._weight}KG")
 
    @classmethod
    def showCount(cls):
        """
        类方法
        :return: none
        """
        print(f"一个创建了Spa类共{cls.cout}次")
 
    @staticmethod
    def showinfo():
        """
        静态方法
        :return:
        """
        print(f"肚子饿了")
 
 
    def __str__(self):
        return f"{self._name}运动第:{self._num}次,吃饭第{self._enum}次之后,体重为:{self._weight}KG"

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
du=HealthSpah.Spa('涂聚文',70)
du.joggin()
du.joggin()
du.joggin()
du.eat()
du.joggin()
du.joggin()
print(du)
print(HealthSpah.Spa.cout)
print(HealthSpah.Spa.showCount())
print(HealthSpah.Spa.cout)
print(HealthSpah.Spa.showinfo())

  

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
44
45
46
47
"""
GameInfo.py
游戏玩家类
edit:geovindu,Geovin Du
date:20230618
IDE:PyCharm 2023.1.2
 
"""
 
import random
import sys
import os
 
class Game(object):
    """
    游戏类
 
    """
    def __init__(self,playerName):
        """
 
        :param playerName:
        """
        self._playerName=playerName
        self._playerScore=0 #记录当前游戏分数
        self._topScore=0   #历史最高分
        self._tatoalScore=0 #总分数
        self._num=0 #玩游戏次数
 
 
    @staticmethod
    def showHelp():
        print("游戏帮助信息")
 
    def showTopScore(self):
        print(f"{self._playerName}玩了{self._num}次,历史最高分是:{self._topScore},总分是{self._tatoalScore}")
 
    def startGame(self):
        self._playerScore = random.randint(1,100)
        if self._playerScore > self._topScore:
            self._topScore = self._playerScore
        self._tatoalScore += self._playerScore
        self._num += 1
        #print(f"{self._playerName}玩了{self._num}游戏,当前积分{self._playerScore}共计分{self._tatoalScore},最高分是:{self._topScore}")
 
    def __str__(self):
        return f"{self._playerName}玩了:{self._num}游戏,最后一次积分是:{self._playerScore}共计分:{self._tatoalScore},最高分是:{self._topScore}"

  

调用:

1
2
3
4
5
6
7
8
ga=GameInfo.Game("李明")
ga.startGame()
ga.startGame()
ga.startGame()
ga.startGame()
ga.showTopScore()
ga.showHelp()
print(ga)

  

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
游戏帮助信息
李明玩了4游戏,当前积分93共计分276,最高分是:93
小明运动第:1次,体重减至69.5 KG
小明运动第:2次,体重减至69.0 KG
小明吃饭第1次之后,体重为:70.0KG
小明运动第:3次,体重减至69.5 KG
小明运动第:3次,吃饭第1次之后,体重为:69.5KG
11
涂聚文运动第:1次,体重减至69.5 KG
涂聚文运动第:2次,体重减至69.0 KG
涂聚文运动第:3次,体重减至68.5 KG
涂聚文吃饭第1次之后,体重为:69.5KG
涂聚文运动第:4次,体重减至69.0 KG
涂聚文运动第:5次,体重减至68.5 KG
涂聚文运动第:5次,吃饭第1次之后,体重为:68.5KG
2
一个创建了Spa类共2
None
2
肚子饿了

  

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
GameInfo.py
游戏玩家类
edit:geovindu,Geovin Du
date:20230618
IDE:PyCharm 2023.1.2
 
"""
 
import random
import time
import sys
import os
 
class Game(object):
    """
    游戏类
    """
 
 
    topGamScrore=0  #
    """
    类属性 最高分
    """
    topGameName="" #
    """
    类属生 最高分的玩家
    """
 
    def __init__(self,playerName):
        """
        构造方法
        :param playerName:
        """
        self._playerName=playerName
        self._playerScore=0 #记录当前游戏分数
        self._topScore=0   #历史最高分
        self._topNmae=''
        self._tatoalScore=0 #总分数
        self._num=0 #玩游戏次数
 
    def __del__(self):
        print(f"{self._playerName}挂机了")
 
    @staticmethod
    def showHelp():
        """
        帮助信息
        :return:
        """
        print(f"游戏帮助信息")
 
 
 
    @staticmethod
    def showTopScroe(cls):
        """
        显示最高分数
        :return:
        """
        #print(f"{self._playerName}玩了{self._num}次,历史最高分是:{self._topScore},总分是{self._tatoalScore}")
        print(f"历史最高分是:{cls.topGamScrore}")
 
    def selfShowTopScore(self):
        """
 
        :return:
        """
        print(f"{self._playerName}玩了{self._num}次,历史最高分是:{self._topScore},总分是{self._tatoalScore}")
    def startGame(self):
        """
        开始玩游戏
        :return:
        """
        self._playerScore = random.randint(1,100)
        if self._playerScore > self._topScore:
            self._topScore = self._playerScore
            self._topNmae=self._playerName
        if self._playerScore >Game.topGamScrore:
            Game.topGamScrore=self._playerScore
            Game.topGameName=self._playerName
        if self._playerScore==Game.topGamScrore:
            Game.topGameName="相同分"
 
        self._tatoalScore += self._playerScore
        self._num += 1
        time.sleep(3)
        #print(f"{self._playerName}玩了{self._num}游戏,当前积分{self._playerScore}共计分{self._tatoalScore},最高分是:{self._topScore}")
 
    def __str__(self):
        """
 
        :return:
        """
        return f"{self._playerName}玩了:{self._num}游戏,最后一次积分是:{self._playerScore}共计分:{self._tatoalScore},最高分是:{self._topScore}"
 
 
 
 
"""
StudentInfo.py
游戏玩家类
edit:geovindu,Geovin Du
date:20230618
IDE:PyCharm 2023.1.2
 
"""
 
class Student(object):
 
 
    def __init__(self,name,age,skill):
        self._name=name
        self._age=age
        self._skill=skill
 
    def showActcion(self):
        print(f"{self._name}表演才艺{self._skill}")
 
    def showInfo(self):
        print(f"大家好,我是{self._name},今年{self._age}岁,才艺{self._skill}")
 
 
    def info(self):
        self.showInfo()
        self.showActcion()
 
 
    def setName(self,name):
        self._name=name
 
    def getName(self):
        return self._name
 
    def setAge(self,age):
        self._age=age
 
    def getAge(self):
        return self._age
 
    def __str__(self):
        return f"大家好,我是{self._name},今年{self._age}岁,才艺{self._skill}"
 
 
 
 
"""
TeachInfo.py
游戏玩家类
edit:geovindu,Geovin Du
date:20230618
IDE:PyCharm 2023.1.2
 
"""
import StudentINfo
 
class Teach(object):
    """
 
    """
    def __init__(self,name):
        """
 
        :param name:
        :param studentName:
        """
        self._name=name
        self._student=None
 
    def intruduce(self,student:StudentINfo.Student):
        """
 
        :return:
        """
        self._student=student
        student.info()
        #print(f"大家好,我是{student.getName()},今年{student.getAge()}岁")
 
    def __str__(self):
        return f"大家好,我是{self._student.getName()},今年{self._student.getAge()}岁"

  

1
2
3
4
5
6
7
8
stu=StudentINfo.Student("小明",18,"唱歌")
stu2=StudentINfo.Student("小军",17,"跳舞")
tea=TeachInfo.Teach("刘老师")
tea.intruduce(stu)
tea.intruduce(stu2)
 
print(tea)
print(stu.showActcion())

  

 

posted @   ®Geovin Du Dream Park™  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2019-06-18 SMTP email from C#
2013-06-18 javascript: Convert special characters to HTML
2013-06-18 javascript:中文等字符转成unicode
2010-06-18 Sql 脚本导入EXCEL数据
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示