python 学习_第三模块 面向对象(初级)
1.定义一个类
# 定义类 class Student: '这个是一个测试' # __doc__ school = 'ww' # 数据属性 def learn(self): # 函数属性 print('is learning ') def eat(self): # 函数属性 print('is sleeping') s = Student() print(Student.__dict__['learn']) # <function Student.learn at 0x0000000001D94950>
2.类的增删改查
class Student: '这个是一个测试' school = 'ww' def learn(self): print('is learning ') def eat(self): print('is sleeping') # 类---> 查 print(Student.__dict__['school']) # ww print(Student.__dict__['eat']) # <function Student.eat at 0x00000000022D2488> # 类 ---> 增加 Student.county = 'china' print(Student.county) # china # 类 ---> 删 del Student.county # 类 ---> 改 Student.school = 'WWW'
3.实例的增删改查
''' 实例的增删改查 ''' class Student: school = 'www' def __init__(self,name,sex,age): self.name=name self.sex=sex self.age=age def learn(self): print('is learning') def eat(self): print('is sleeping') # 实例化对象 s = Student('yy','男',22) # Student.__init__(s,'yy','男',22) # 实例化的步骤 # 1 先产生一个空对象 s # 2 Student.__init__(s,'yy','男',22) # 查 print(s.__dict__) # {'name': 'yy', 'sex': '男', 'age': 22} # 改 s.name = 'yangyang' print(s.__dict__) # {'name': 'yangyang', 'sex': '男', 'age': 22} # 删除 del s.name print(s.__dict__) # {'sex': '男', 'age': 22} # 增加 s.class_name = 'python' print(s.__dict__) # {'sex': '男', 'age': 22, 'class_name': 'python'}
4.小结
""" 类成员: 属性: 数据属性 (保存在类中) 对象 类都可以访问 函数属性 只能 对象访问 方法: 普通方法(保存在类中) 由对象来调用 self 代指 对象 静态方法 @staticmetic 类方法 cls当前类 @classmetic 当对象中需要保存一些值,执行某些功能时,需要使用对象的值 ----> 普通方法 不需要任何对象中的值 ----> 静态方法 """
5.属性查找
''' 属性查找 ''' class Student: school='www' def __init__(self,name,sex,age): self.Name=name self.Sex=sex self.Age=age def learn(self): print('%s is learning ' %(self.Name)) stu1=Student('王二丫','女',18) # 类中的函数属性:是绑定给对象使用的,绑定到不同的对象是不同的绑定方法,对象调用绑定方式时,会把对象本身当作第一个传入,传给self # 类的属性查找 Student.learn(stu1) # 王二丫 is learning # 对象调用 stu1.learn() # 王二丫 is learning
6.练习
# ''' # 练习1:编写一个学生类,产生一堆学生对象, (5分钟) # 要求: # 有一个计数器(属性),统计总共实例了多少个对象 # ''' class Student: count = 0 def __init__(self,name,age): self.name = name self.age = age Student.count += 1 s = Student('y',11) print(Student.count) s2 = Student('yy',11) print(Student.count) ''' 练习2:模仿LoL定义两个英雄类, (10分钟) 要求: 英雄需要有昵称、攻击力、生命值等属性; 实例化出两个英雄对象; 英雄之间可以互殴,被殴打的一方掉血,血量小于0则判定为死亡。 ''' class People: 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 -= self.aggresivity # g1.life_value -= r1.aggresivity class Riven(People): camp = 'Noxus' class Garen(People): camp = 'Demacia' g1=Garen('草丛伦',100,30) r1=Riven('可爱的锐雯雯',80,50) print(r1.life_value) g1.attack(r1) print(r1.life_value)
7. 继承
# 继承 有新式类 经典类 # 新式类 是广度优先 Python3 Python2 Python(object) 这是广度优先的 # 经典类 是深度优先 只有Python 有深度优先 一路走到黑 class A: # def test(self): # print('from A') pass class B(A): # def test(self): # print('from B') pass class C(A): # def test(self): # print('from C') pass class D(B): # def test(self): # print('from D') pass class E(C): # def test(self): # print('from E') pass class F(D,E): # def test(self): # print('from F') pass # 新式类 的继承顺序 广度优先 #F --> D --> B --> E --> C --> A #print(F.mro()) # 经典类 的继承顺序 深度优先 # F --> D --> B --> A--> E --> C
8.属性查找
#属性查找小练习 class Foo: def f1(self): print('from Foo.f1') def f2(self): print('from Foo.f2') self.f1() #b.f1() class Bar(Foo): def f1(self): print('from Bar.f1') b=Bar() # print(b.__dict__) b.f2()
9.子类中重用父类的属性
#在子类派生出的新的方法中重用父类的方法,有两种实现方式 #方式一:指名道姓(不依赖继承) class Hero: 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-=self.aggresivity class Garen(Hero): camp='Demacia' def attack(self,enemy): Hero.attack(self,enemy) #指名道姓 print('from Garen Class') class Riven(Hero): camp='Noxus' g=Garen('草丛伦',100,30) r=Riven('锐雯雯',80,50) print(r.life_value) g.attack(r) print(r.life_value) # 方式二:super() (依赖继承) class Hero: 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-=self.aggresivity class Garen(Hero): camp='Demacia' def __init__(self,nickname,life_value,aggresivity,weapon): super().__init__(nickname,life_value,aggresivity) self.weapon=weapon def attack(self,enemy): Hero.attack(self,enemy) #指名道姓 print('from Garen Class') g=Garen('草丛伦',100,30,'金箍棒') print(g.__dict__) # 3 class A: def f1(self): print('from A') super().f1() class B: def f1(self): print('from B') class C(A,B): pass print(C.mro()) #[<class '__main__.C'>, # <class '__main__.A'>, # <class '__main__.B'>, # <class 'object'>] c=C() c.f1()
posted on 2019-04-03 16:34 augustyang 阅读(185) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步