标签:总结 攻击 print 调用 int 输出 而且 sel 报错
在现实中:先有对象,才有类
在程序中:一定先定义类,后调用类来产生对象
‘‘‘
在现实世界中:
对象1:王二丫
特征:
学校=‘luffycity‘
名字=‘王二丫‘
性别=‘女‘
年龄=18
技能:
学习
吃饭
睡觉
对象2:李三炮
特征:
学校=‘luffycity‘
名字=‘李三炮‘
性别=‘男‘
年龄=38
技能:
学习
吃饭
睡觉
对象3:张铁蛋
特征:
学校=‘luffycity‘
名字=‘张铁蛋‘
性别=‘男‘
年龄=48
技能:
学习
吃饭
睡觉
总结现实中路飞学院的学生类:
相似的特征
学校=‘luffycity‘
相似的技能
学习
吃饭
睡觉
‘‘‘
#定义类
class LuffyStudent():
school = ‘luffycity‘ #类的数据属性
def learn(self):#类的函数属性
pass
def eat(self):
pass
def sleep(self):
pass
#产生对象
stu1 = LuffyStudent()
stu2 = LuffyStudent()
stu3 = LuffyStudent()
#查看类的名称空间
# print(LuffyStudent.__dict__)
#查
print(LuffyStudent.school)
#增
# LuffyStudent.county = ‘China‘
# print(LuffyStudent.county)
#删
# del LuffyStudent.county
# print(LuffyStudent.county)
#改
LuffyStudent.school = ‘Luffycity‘
print(LuffyStudent.school)</pre>
__init__方法主要用来为对象定制对象独有的特性
#定义类
class LuffyStudent():
school = ‘luffycity‘
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def learn(self):
print(‘is learn‘)
def eat(self):
print(‘is eat‘)
def sleep(self):
print(‘is sleep‘)
#产生对象
stu1 = LuffyStudent(‘王二丫‘,29,‘女‘)
#先调用类产生空对象stu1,然后调用LuffyStudent.__init__(stu1,‘王二丫‘,29,‘女‘)
stu2 = LuffyStudent(‘张三‘,18,‘男‘)
stu3 = LuffyStudent(‘李四‘,28,‘女‘)
#查
# print(stu1.age)
# #删
# del stu1.age
# print(stu1.age)
#增
stu1.class_name = ‘Python开发‘
print(stu1.class_name)
print(stu1.__dict__)
#改
stu1.age = 19
print(stu1.age)
类有两种属性:
* 类的数据属性,属于对象共用的
print(id(stu1.school))
print(id(stu2.school))
print(id(stu3.school))
#输出
2336910776112
2336910776112
2336910776112
* 类的函数属性是绑定给对象使用的,称为绑定对象的方法
print(LuffyStudent.sleep)
print(stu1.sleep)
print(stu3.sleep)
print(stu2.sleep)
#输出
<function LuffyStudent.sleep at 0x000001EAB2BAAD90>
<bound method LuffyStudent.sleep of <__main__.LuffyStudent object at 0x000001EAB2BCC780>>
<bound method LuffyStudent.sleep of <__main__.LuffyStudent object at 0x000001EAB2BCC828>>
<bound method LuffyStudent.sleep of <__main__.LuffyStudent object at 0x000001EAB2BCC7F0>>
在obj.name会先从obj自己的名称空间里找name,找不到则去类中找,类也找不到就去父类里找,父类找不到就报错了
练习1:编写一个学生类,产生一堆学生对象, (5分钟)
要求:
class Student:
school = ‘luffycity‘
cont = 0
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
Student.cont += 1
def learn(self):
pass
stu1 = Student(‘张三‘,18,‘男‘)
stu2 = Student(‘李四‘,18,‘男‘)
stu3 = Student(‘王五‘,18,‘男‘)
print(Student.cont)
练习2:模仿王者荣耀定义两个英雄类, (10分钟)
要求:
class zhugeliang:
camp = ‘red‘
def __init__(self,nickname,life_value,aggresivity):
self.nickname = nickname
self.life_value = life_value
self.aggresivity = aggresivity
def attack(self,enemy):
enemy.life_value -= enemy.aggresivity
if enemy.life_value <= 0:
print(‘%s已死亡!‘%enemy.nickname)
class xiaoqiao:
camp = ‘blue‘
def __init__(self,nickname,life_value,aggresivity):
self.nickname = nickname
self.life_value = life_value
self.aggresivity = aggresivity
def attack(self,enemy):
enemy.life_value -= enemy.aggresivity
if enemy.life_value <= 0:
print(‘%s已死亡!‘ % enemy.nickname)
r1 = zhugeliang(‘诸葛亮‘,200,100)
g1 = xiaoqiao(‘小乔‘,300,90)
g1.attack(r1)
g1.attack(r1)
标签:总结 攻击 print 调用 int 输出 而且 sel 报错
原文地址:https://www.cnblogs.com/yjiu1990/p/9122778.html