面向对象编程
#类 和对象
class Stu():
name=""
sex=""
score=0
def dayin(self):
print(self.name,self.sex,self.score)
zs=Stu()
zs.name="张三"
zs.sex="男"
zs.score=99
zs.dayin()#张三 男 99
#定义一个简单类,只包含方法
class Dog():
def sleep(self):
print("睡觉")
def eat(self):
print("吃饭")
snopy=Dog()
snopy.sleep()#睡觉
snopy.eat()#吃饭
#__init__方法,用于初始化操作
class People():
def __init__(self,name,sex):
self.name=name
self.sex=sex
def dayin(self):
print(self.name,self.sex)
zs=People("张三","男")
zs.dayin()#张三 男
#在类的外部增加属性(虽然简单,但不建议使用)
class Person():
def dayin(self):
print(self.name)
zs=Person()
zs.name="张三"
zs.dayin()#张三
#面向对象的综合小练习
class Circle():
def __init__(self,radius):
self.radius=radius
def zhouchang(self):
return 2*3.14*self.radius
def area(self):
return 3.14*self.radius*self.radius
x=Circle(2)
print(x.zhouchang())#12.56
print(x.area())#12.56
#类属性和实例属性
class Stu():
count=23#类属性,放在方法外面
def __init__(self,name,age):#self.属性。全是实例属性(对象属性)
self.name=name
self.age=age
def show(self):
print(self.name,self.age)
print(Stu.count)#23
zs=Stu("张三",18)
zs.show()#张三 18
class Teacher():
count=0
def __init__(self,name,salary):
self.name=name
self.salary=salary
Teacher.count+=1
def show(self):
print(self.name,self.salary)
print(Teacher.count)#0
zs=Teacher("张三",10000)
print(Teacher.count)#1
ls=Teacher("李四",19000)
print(Teacher.count)#2
**# #当类属性和对象属性重名的时候优先使用对象属性(类似于局部变量与全局变量的关系)**
# zs.count=100
# print(zs.count)#100
# zs.count=zs.count+5
# print(zs.count)#7
# print(Teacher.count)#2
# del zs.count
# print(zs.count)#2
# 对象是可变类型的话,修改里面的数据都会有影响
class Stu():
count=0
stu_List=[1,2,3]
def __init__(self,name):
self.name=name
Stu.count+=1
def show(self):
print(self.name)
ZS=Stu("张三")
ZS.stu_List.append(4)
Stu.stu_List.append(5)
print(ZS.stu_List)#[1, 2, 3, 4, 5]
print(Stu.stu_List)#[1, 2, 3, 4, 5]
#类方法和静态方法
class Teacher():
count=8
def __init__(self,name,age):
self.name=name
self.age=age
def say(self):
print(self.name,self.age)
@classmethod
def x(cls):
print(id(cls))
print(id(Teacher)) #49582680
print(Teacher.count) #49582680
print("这是一个类方法")
@staticmethod
def he(a,b):
c=a+b
print(c)
print("这是一个静态方法")
ww=Teacher("王五",23)
ww.say()
Teacher.x()#8 这是一个类方法
Teacher.he(2,3)#5 这是一个静态方法
练习:
定义一个教师类:
类属性:
1、求教师的个数
2、税率,每个老师的税率都是0.002
初始化对象属性:name、salary
类方法:
打印总共定义了多少个老师对象
静态方法:
传入两个值,分别是男老师的工资,和女老师的工资,求两人一共缴纳多少税
class Teach**er():
count=0
tex=0.002
def __init__(self,name,salary):
self.name=name
self.salary=salary
Teacher.count+=1
@classmethod
def dayin(cls):
print(Teacher.count)
@staticmethod
def x(a,b):
c=(a+b)*Teacher.tex
print(c)
li=Teacher("丽丽",12000)
tom=Teacher("汤姆",15000)
Teacher.dayin()#2
Teacher.x(li.salary,tom.salary)#54.0
#__str__方法,默认返回对象的内存地址
class Cat():
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self):
return "这只猫叫{},今年{}岁。".format(self.name,self.age)
tom=Cat("Tom",2)
print(tom)#这只猫叫Tom,今年2岁。
b=str(tom)
print(b)#这只猫叫Tom,今年2岁。
day9作业
# 面向对象中 属性的练习
1、奔跑吧兄弟
#邓超 体重 75.0 公斤
#邓超每次 奔跑 会减肥 0.1 公斤
#邓超每次 吃东西 体重增加 0.5 公斤
class Person():
def __init__(self,name,weight):
self.name=name
self.weight=weight
def __str__(self):
return "我叫{},体重{}".format(self.name,self.weight)
def eat(self):
print("吃饱了才有力气减肥")
self.weight+=0.5
def run(self):
print("只要跑得快,脂肪追不上")
self.weight-=0.1
dc=Person("邓超",77)
dc.eat()
dc.run()
dc.run()
print(dc)
# 两个类配合使用练习
2、狙击手
需求
狙击手 麦克 有一把 狙M99
麦克 可以 开火
狙M99 能够 发射 子弹
狙M99 装填 装填子弹 —— 增加子弹数量
class Gun():
def __init__(self,name,bullet_count):
self.name=name
self.bullet_count=bullet_count
def add(self):
self.bullet_count+=5
def shoot(self):
if self.bullet_count==0:
print("没有子弹")
return
self.bullet_count-=1
print("{}开枪,剩余子弹数量{}".format(self.name,self.bullet_count))
class Sniper():
def __init__(self,name):
self.name=name
self.gun=None
def fire(self):
if self.gun is None:
print("{}没有枪".format(self.name))
return
print("{}射击".format(self.name))
self.gun.add()
self.gun.shoot()
gun=Gun("M99",10)
sniper=Sniper("麦克")
sniper.gun=gun
sniper.fire()
# 静态方法类方法使用
3、植物大战僵尸
class Game():
top_Score=0
def __init__(self,gamer):
self.gamer=gamer
@staticmethod
def help():
print("友情提示:不要让僵尸闯过来")
@classmethod
def show_top_Score(cls):
print("历史记录{}".format(cls.top_Score))
def start_game(self):
print("游戏开始了欢迎玩家{}".format(self.gamer))
tom=Game("TOM")
tom.help()
tom.show_top_Score()
tom.start_game()