Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
lesson14_1
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
cf4e99dd
authored
Apr 23, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
a2cc0669
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
0 deletions
diy2.py
diy3.py
diy2.py
View file @
cf4e99dd
class
Hero
:
# 类名,Hero表示英雄
def
__init__
(
self
,
name
,
hp
,
attack
):
# 用关键字def定义一个 __init__方法,参数:(self)
self
.
level
=
1
# 等级
self
.
name
=
name
# 名字(类属性)
self
.
hp
=
hp
# 生命(类属性)
self
.
attack
=
attack
# 攻击力(类属性)
def
ypgrade
():
yase
.
level
=
yase
.
level
+
1
yese
.
hp
=
yase
.
hp
+
50
yase
.
attack
=
yase
.
attack
+
4
yase
=
Hero
(
"亚瑟"
,
300
,
20
)
print
(
"亚瑟的初始血量为: "
,
yase
.
hp
)
upgrade
()
upgrade
()
print
(
"升级2次后,亚瑟的血量为: "
,
yase
.
hp
)
\ No newline at end of file
diy3.py
View file @
cf4e99dd
class
Hero
:
# 类名,Hero表示英雄
def
__init__
(
self
,
name
,
hp
,
attack
):
# 用关键字def定义一个 __init__方法,参数:(self)
self
.
level
=
1
# 等级
self
.
name
=
name
# 名字(类属性)
self
.
hp
=
hp
# 生命(类属性)
self
.
attack
=
attack
# 攻击力(类属性)
def
ypgrade
():
# 用关键字def定义方法名,封装函数
yase
.
level
=
yase
.
level
+
1
# 每升级一次等级+1
yese
.
hp
=
yase
.
hp
+
50
# 。。。。 血量+50
yase
.
attack
=
yase
.
attack
+
4
# 。。。。 攻击力+4
yase
=
Hero
(
"亚瑟"
,
300
,
20
)
# 括号内实参123分别对应形参123
print
(
"亚瑟的初始血量为: "
,
yase
.
hp
)
upgrade
()
# 重复调用函数
upgrade
()
# 重复调用函数
print
(
"升级2次后,亚瑟的血量为: "
,
yase
.
hp
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment