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
e897c684
authored
Mar 01, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
f47689a4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
0 deletions
1.py
diy.py
1.py
0 → 100644
View file @
e897c684
class
Hero
:
def
__init__
(
self
,
name
):
#函数,无参函数 有参函数 形参是变量
self
.
level
=
1
self
.
hp
=
300
self
.
attack
=
40
self
.
name
=
name
def
upgrade
(
self
):
self
.
level
+=
1
self
.
hp
+=
200
self
.
attack
+=
30
#创建攻击方法
def
combat
(
self
,
dr
):
dr
.
hp
-=
self
.
attack
#?对?进行了攻击
info1
=
self
.
name
+
"对"
+
dr
.
name
+
"进行了攻击"
#受到了?点伤害
info2
=
dr
.
name
+
"受到了"
+
str
(
self
.
attack
)
+
"点伤害"
#判断敌人的血量是否大于0
if
dr
.
hp
>
0
:
#?还剩?点血量
info3
=
dr
.
name
+
"还剩"
+
str
(
dr
.
hp
)
+
"点血量"
print
(
info1
+
info2
+
info3
)
else
:
#?已阵亡,游戏结束
info3
=
dr
.
name
+
"已阵亡,游戏结束"
class
player
(
Hero
):
def
__init__
(
self
,
name
,
HeroType
):
super
()
.
__init__
(
name
)
self
.
hp
=
500
self
.
attack
=
60
self
.
HeroType
=
HeroType
#?的珍贵程度是:
print
(
self
.
name
,
"的珍贵程度是:"
,
self
.
HeroType
)
#?的等级,血量和攻击力分别是:等级,血量,攻击力
print
(
self
.
name
,
"的等级,血量和攻击力分别是:"
,
self
.
level
,
self
.
hp
,
self
.
attack
)
houyi
=
player
(
"导向蓟"
,
"white"
)
#实例化对象 实参是一个具体数值
js
=
Hero
(
"跳舞僵尸"
)
houyi
.
combat
(
js
)
diy.py
View file @
e897c684
类:具有相同特征的一个群体
比如:学生,爬行动物
对象:
对象是对类的一个实例化,类中的一个具体的事物
属性:静态的,描述他的特征的
方法:动态的,描述他的行为的
class
类的名字:
变量赋值 #属性
def
__init
(
self
)
__
:
初始化方法
\ 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