day06 面向对象编程
面向对象:





多态:





就是把 r1 这个变量名传进去类里了

self 就是你创建的对象的变量名

实践:
class Role:
def __init__(self,name,role,weapon,life_value=100,money=15000):
self.name = name
self.role = role
self.weapon = weapon
self.life_value = life_value
self.money = money
def shot(self):
print("shotting ...")
def got_shot(self):
print("ah .... ,i got shot ...")
def buy_gun(self,gun_name):
print("{_name} buy the {_gun_name} gun".format(_name=self.name,_gun_name=gun_name))
r1 = Role("Alex","F","ak-47")
r2 = Role("小明","T","m4-a1")
r1.buy_gun("m4")
r2.got_shot()
运行结果:


类变量:

新加变量:

默认去找实例变量,找不到实例变量才去找类变量
r1添加变量,r2 实例化不变,因为是r1 添加变量

改变类变量,如果实例里没有那个变量,那么就会跟着改变

对于list 会导致所有的改变

class Role:
n = 123
n_lsit = []
def __init__(self,name,role,weapon,life_value=100,money=15000):
self.name = name
self.role = role
self.weapon = weapon
self.life_value = life_value
self.money = money
def shot(self):
print("shotting ...")
def got_shot(self):
print("ah .... ,i got shot ...")
def buy_gun(self,gun_name):
print("{_name} buy the {_gun_name} gun".format(_name=self.name,_gun_name=gun_name))
r1 = Role("Alex","F","ak-47")
r2 = Role("小明","T","m4-a1")
# r1.buy_gun("m4")
#
# r2.got_shot()
print("r1",r1.n,r1.n_lsit)
print("r2",r2.n,r2.n_lsit)
r1.n = "heh1"
r2.n = "hah2"
print("Class:",Role.n,Role.n_lsit)
r1.n_lsit.append("r11")
print("r1",r1.n,r1.n_lsit)
r2.n_lsit.append("r22")
print("r2",r2.n,r2.n_lsit)
print("Class:",Role.n,Role.n_lsit)
print("r1",r1.n,r1.n_lsit)
运行结果:


写一个:


如果在这之前就del 那么就会提前执行,也就是这个会在一个实例销毁时执行的


外面想要查看,那么就定义一个函数然后在外面来调用这个函数即可

当然外面的不能访问,必然不能修改,所以想修改就要在里面定义好修改的方式,如下:


实践 私有属性:
class Role:
n = 123
n_lsit = []
def __init__(self,name,role,weapon,life_value=100,money=15000):
self.name = name
self.role = role
self.weapon = weapon
self.__life_value = life_value
self.money = money
def shot(self):
print("shotting ...")
def got_shot(self):
print("ah .... ,i got shot ...")
def show_life_value(self):
print("{_name}'s life value is {_life_value}".format(_name=self.name,
_life_value=self.__life_value))
def buy_gun(self,gun_name):
print("{_name} buy the {_gun_name} gun".format(_name=self.name,_gun_name=gun_name))
r1 = Role("Alex","F","ak-47")
r2 = Role("小明","T","m4-a1")
r1.buy_gun("m4")
r2.got_shot()
r1.show_life_value()
r2.show_life_value()


正确方式就是在类里定义另一个方法来调用这个方法:

class Role:
n = 123
n_lsit = []
def __init__(self,name,role,weapon,life_value=100,money=15000):
self.name = name
self.role = role
self.weapon = weapon
self.__life_value = life_value
self.money = money
def shot(self):
print("shotting ...")
def __got_shot(self):
print("ah .... ,i got shot ...")
self.__life_value -= 50
def show_life_value(self):
print("{_name}'s life value is {_life_value}".format(_name=self.name,
_life_value=self.__life_value))
def buy_gun(self,gun_name):
print("{_name} buy the {_gun_name} gun".format(_name=self.name,_gun_name=gun_name))
def out_shot(self):
print("调用里面的私有方法被射中...")
self.__got_shot()
r2 = Role("小明","T","m4-a1")
r2.out_shot()
r2.show_life_value()

增加新功能,并先调用父类方法:


这就等于重构了父类的方法
创建一个woman:

实践:
class People:
def __init__(self,name,age):
self.name = name
self.age = age
def eat(self):
print("{_name} is eatting ...".format(_name=self.name))
def sleep(self):
print("{_name} is sleepping ...".format(_name=self.name))
class Man(People):
def piao(self): #添加新的方法,这个方法只能是这个类来用
print("{_name} can piaoing ...".format(_name=self.name))
def sleep(self): #修改父类里的方法,添加功能,实现个性化
People.sleep(self)
print("给睡觉添加新的功能")
class Woman(People):
def sheng(self): #添加新的方法,这个方法只能是这个类来用
print("{_name} can shenghaizi ...".format(_name=self.name))
m1 = Man("小明",22)
w1 = Woman("小花",18)
print("m1")
m1.sleep()
print("w1")
w1.sleep()
print("m1")
m1.piao()
print("w1")
w1.sheng()

父类的所有参数都要写一遍,写完后面加自己的初始化变量

class People:
def __init__(self,name,age):
self.name = name
self.age = age
def eat(self):
print("{_name} is eatting ...".format(_name=self.name))
def sleep(self):
print("{_name} is sleepping ...".format(_name=self.name))
class Action(object):
def make_friends(self,object):
print("{_name} is making from {_obj_name}".format(_name=self.name,
_obj_name=object.name))
class Man(People,Action):
def piao(self): #添加新的方法,这个方法只能是这个类来用
print("{_name} can piaoing ...".format(_name=self.name))
def sleep(self): #修改父类里的方法,添加功能,实现个性化
People.sleep(self)
print("给睡觉添加新的功能")
class Woman(People):
def sheng(self): #添加新的方法,这个方法只能是这个类来用
print("{_name} can shenghaizi ...".format(_name=self.name))
m1 = Man("小明",22)
w1 = Woman("小花",18)
m1.make_friends(w1)


扩展版:
#!/usr/bin/env python3
# Author: Shen Yang
class People:
def __init__(self,name,age):
self.name = name
self.age = age
def eat(self):
print("{_name} is eatting ...".format(_name=self.name))
def sleep(self):
print("{_name} is sleepping ...".format(_name=self.name))
class Action(object):
def make_friends(self,object):
print("{_name} is making from {_obj_name}".format(_name=self.name,
_obj_name=object.name))
self.firends.append(object)
def pay_money(self,object):
print("{_name} giveing {_obj_name} {_money} money".format(_name=self.name,
_obj_name=object.name,
_money=self.money))
class Man(People,Action):
firends = []
def __init__(self,name,age,money):
super(Man,self).__init__(name,age)
self.money = money
def piao(self): #添加新的方法,这个方法只能是这个类来用
print("{_name} can piaoing ...".format(_name=self.name))
def sleep(self): #修改父类里的方法,添加功能,实现个性化
People.sleep(self)
print("给睡觉添加新的功能")
class Woman(People):
def sheng(self): #添加新的方法,这个方法只能是这个类来用
print("{_name} can shenghaizi ...".format(_name=self.name))
m1 = Man("小明",22,6800)
w1 = Woman("小花",18)
m1.make_friends(w1)
m1.pay_money(w1)
w1.name ="小花花"
print(m1.firends[0].name)


#!/usr/bin/env python3
# Author: Shen Yang
#object 是所有类的类:
class School(object):
def __init__(self,name,addr):
self.name = name
self.addr = addr
self.students = []
self.staffs = []
def enroll(self,stu_obj):
print("为{_stu_obj} 办理了入学手续。。。".format(_stu_obj=stu_obj.name))
self.students.append(stu_obj)
def hire(self,staff_obj):
print("雇佣了新员工 {_staff_obj}".format(_staff_obj=staff_obj.name))
self.staffs.append(staff_obj)
class SchoolMember(object):
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def tell(self):
pass
class Student(SchoolMember):
def __init__(self,name,age,sex,stu_id,grade):
super(Student,self).__init__(name,age,sex)
self.stu_id = stu_id
self.grade = grade
def tell(self):
print('''
----- info of Student: {_name} -----
Name:{_name}
Age:{_age}
Sex:{_sex}
Stu_id:{_stu_id}
Grade:{_grade}
'''.format(_name=self.name,_age=self.age,_sex=self.sex,_stu_id=self.stu_id,_grade=self.grade))
def pay_tuition(self,amount):
print("{_name} has paid tuition {_amount}".format(_name=self.name,_amount=amount))
class Teacher(SchoolMember):
def __init__(self,name,age,sex,salary,coure):
super(Teacher,self).__init__(name,age,sex)
self.salary = salary
self.coure = coure
def tell(self):
print('''
----- info of Teacher: {_name} -----
Name:{_name}
Age:{_age}
Sex:{_sex}
Salary:{_salary}
Coure:{_coure}
'''.format(_name=self.name,_age=self.age,_sex=self.sex,_salary=self.salary,_coure=self.coure))
def teach(self):
print("{_name} has teach {_coure}".format(_name=self.name,_coure=self.coure))
#初始化一个学校
school = School("老男孩","沙河")
#初始化两个教师
t1 = Teacher("Oldboy",35,"M",20000,"Linux")
t2 = Teacher("Alex",22,"M",3000,"Python")
#初始化一个学生
s1 = Student("Yang",26,"M",1001,"Python")
#让第一个老师打印信息然后开始教课
t1.tell()
t1.teach()
#让第二个老师打印信息
t2.tell()
#让学生打印信息然后缴费
s1.tell()
s1.pay_tuition(6888)
#学校给学生办理入学手续
school.enroll(s1)
#打印学校学生
print(school.students[0].name)
school.hire(t1)
school.hire(t2)
for i in school.staffs:
print(i.name)


day06 面向对象编程的更多相关文章
- Java面向对象编程基础
一.Java面向对象编程基础 1.什么是对象?Object 什么都是对象! 只要是客观存在的具体事物,都是对象(汽车.小强.事件.任务.按钮.字体) 2.为什么需要面向对象? 面向对象能够像分析现实生 ...
- angular2系列教程(六)两种pipe:函数式编程与面向对象编程
今天,我们要讲的是angualr2的pipe这个知识点. 例子
- 带你一分钟理解闭包--js面向对象编程
上一篇<简单粗暴地理解js原型链--js面向对象编程>没想到能攒到这么多赞,实属意外.分享是个好事情,尤其是分享自己的学习感悟.所以网上关于原型链.闭包.作用域等文章多如牛毛,很多文章写得 ...
- PHP 面向对象编程和设计模式 (1/5) - 抽象类、对象接口、instanceof 和契约式编程
PHP高级程序设计 学习笔记 2014.06.09 什么是面向对象编程 面向对象编程(Object Oriented Programming,OOP)是一种计算机编程架构.OOP 的一条基本原则是计算 ...
- Delphi_09_Delphi_Object_Pascal_面向对象编程
今天这里讨论一下Delphi中的面向对象编程,这里不做过多过细的讨论,主要做提纲挈领的描述,帮助自己抓做重点. 本随笔分为两部分: 一.面向对象编程 二.面向对象编程详细描述 ------------ ...
- python基础-面向对象编程
一.三大编程范式 编程范式即编程的方法论,标识一种编程风格 三大编程范式: 1.面向过程编程 2.函数式编程 3.面向对象编程 二.编程进化论 1.编程最开始就是无组织无结构,从简单控制流中按步写指令 ...
- 面向对象编程(OOP)
什么是面向对象编程,对于面向对象编程与面向过程编程的解释随处可见,个人认为对面向对象编程解释最好的一个定义是:依赖倒转原则是面向对象编程的标志,面向对象编程是一种思想,无论使用哪一种编程语言,如果在编 ...
- python 学习笔记7 面向对象编程
一.概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发"更快更好更强..." ...
- 进击的Python【第七章】:Python的高级应用(四)面向对象编程进阶
Python的高级应用(三)面向对象编程进阶 本章学习要点: 面向对象高级语法部分 静态方法.类方法.属性方法 类的特殊方法 反射 异常处理 Socket开发基础 一.面向对象高级语法部分 静态方法 ...
随机推荐
- MariaDB 实现主从复制
實驗目的: MariaDB為MySQL的一個分支,其完全開源.無版權之虞且操作上與 MySQL 一脈相承,實際應用中非常廣泛,軟件本身很小,安裝容易,使用簡單. 但其也有缺點,指令行方式操作,無原生G ...
- H5的storage(sessionstorage&localStorage)简单存储删除
众所周知,H5的storage有sessionstorage&localStorage,其中他们的共同特点是API相同 下面直接上代码,storage中的存储与删除: <!DOCTYPE ...
- The first step in solving any problem is recognizing there is one.
The first step in solving any problem is recognizing there is one.解决问题的第一步是要承认确实存在问题.
- ajax提交表单无法验证easyui的验证选项(比如required等)
在实际开发中,遇到ajax方式提交表单没法验证easyui的验证选项,这对实际用户体验造成了很大的困扰.当然,这也是理所当然的事情. 解决办法:使用jquery中ajax的beforeSend事件 ...
- LR脚本示例之常用函数
1.变量和参数的设置 //将IP地址和端口放入到参数中lr_save_string("127.0.0.1:1080","ip"); //退出脚本建议使用lr_e ...
- Lodash.js常用拷贝
lodash.js 降低 array.number.objects.string 等等的使用难度从而让 JavaScript 变得更简单.非常适用于:遍历 array.object 和 string: ...
- 【Python图像特征的音乐序列生成】关于数据库到底在哪里下载
毕竟原网站一个是14年前的一个是16年前的…… 1,http://ifdo.ca/~seymour/nottingham/nottingham.html 这个网站可以下载zip包. 2,https:/ ...
- Uva 10635 Prince and Princess (LCS变形LIS)
直接LCS是时间复杂度是O(p*q)的,但是序列元素各不相同,只要把其中一个序列映射成有序的, 另外一个序列再做相同的映射,没有的直接删掉,就变成了求另一个序列LIS. #include<bit ...
- Android(java)学习笔记112:Activity中的onCreate()方法分析
1.onCreate( )方法是android应用程序中最常见的方法之一: 翻译过来就是说,onCreate()函数是在activity初始化的时候调用的,通常情况下,我们需要在onCreate()中 ...
- ubuntu下安装eclipse<转>
转载自http://my.oschina.net/u/1407116/blog/227084 http://my.oschina.net/u/1407116/blog/227087 一 JD ...