day19_python_1124
.01 昨日内容回顾
面向对象:1,将一些相似功能的函数集合到一起
类:具有相同属性和功能的一类事物。
对象:类的具体体现。
2,站在上帝的角度考虑问题,类就是一个公共模板,
类的结构:
class Person:
mind = '有思想'静态属性 静态字段
def __init__(self,name,age):
self.name = name
self.age = age
def work(self): # 动态属性 方法
print('能工作')
类名:
1,Person.mind万能的点.
2, Person.__dict__ 查询类中的所有内容
不建议这么做
3,Person.work(111)
对象:
obj = Person('barry', 18)
1,在内存中开辟一个空的对象空间
2,自动执行__init__方法,先把对象空间传给self,后面参数一次传入
3,执行__init__代码,将封装好属性的对象空间返回给了obj
1,查询对象的属性:
obj.__dict__
2,查询对象单独的属性:
obj.name
3,对象查询类中的属性:
obj.mind
4,对象执行类中的方法:
obj.work()
Person.work(obj)
02 作业讲解
03 类的名称空间以及对象名称空间
04 组合
05 面向对象的三大特征:继承,封装,多态
# 03 类名称空间对象空间
# class Gamerole(object):
# name = 'LOL' # year = 2012 # def __init__(self,name,ad,hp): # self.name = name # self.ad = ad # self.hp = hp # def attack(self,p1): # p1.hp -= self.ad # print('%s攻击%s,%s掉了%s血,还剩%s血'%(self.name,p1.name,p1.name,self.ad,p1.hp))
# gailun = Gamerole('草丛伦',40,1200)# gailun.armor = 60 #给对象添加属性# print(gailun.__dict__)# 对象的属性:不仅能在__init__里面添加,还可以在其他方法或者类外面添加。# yasuo = Gamerole('托儿所',60,800)# yasuo.attack(gailun)# 类的属性:不仅在类内部可以添加,还可以在类外部添加。# Gamerole.year = 6# print(Gamerole.__dict__)
# print(gailun.year) #从对象查看类中属性# 对象空间中存在一个类对象指针,所以对象可以找到类中的变量以及方法# 类名只能找到类中的变量,方法,或者(父类中的),不能找对象中的属性。
# 04 组合
# 计算一个类被多少人执行过。
'''count = 0class A: def __init__(self): global count count += 1''' #建议使用下面方法# class A:# count = 0# def __init__(self):# A.count += 1 # *** ## obj = A()# print(obj.count)# obj = A()# print(obj.count)# obj = A()# obj = A()# obj = A()# obj = A()# print(A.count)
# 组合: 将一个对象封装到另一个对象的属性中
# class Gamerole:# def __init__(self,name,ad,hp):# self.name = name# self.ad = ad# self.hp = hp# def attack(self,p1):# p1.hp -= self.ad# print('%s攻击%s,%s掉了%s血,还剩%s血'%(self.name,p1.name,p1.name,self.ad,p1.hp))## def equip_weapon(self,wea):## self.wea = wea
# class Weapon:# def __init__(self,name,ad):# self.name = name# self.ad = ad# def weapon_attack(self,p1,p2):# p2.hp = p2.hp - self.ad - p1.ad# print('%s 利用 %s 攻击了%s,%s还剩%s血' %(p1.name,self.name,p2.name,p2.name,p2.hp))
# barry = Gamerole('太白',10,200)# panky = Gamerole('金莲',20,100)# pillow = Weapon('枕头',2)
# pillow.weapon_attack(barry,panky)
# class Gamerole:# def __init__(self,name,ad,hp):# self.name = name# self.ad = ad# self.hp = hp# def attack(self,p1):# p1.hp -= self.ad# print('%s攻击%s,%s掉了%s血,还剩%s血'%(self.name,p1.name,p1.name,self.ad,p1.hp))# def equip_weapon(self,wea):# self.wea = wea # 组合:给一个对象封装一个属性,该属性是另一个类的对象
# class Weapon:# def __init__(self,name,ad):# self.name = name# self.ad = ad# def weapon_attack(self,p1,p2):# p2.hp = p2.hp - self.ad - p1.ad# print('%s 利用 %s 攻击了%s,%s还剩%s血' %(p1.name,self.name,p2.name,p2.name,p2.hp))
# barry = Gamerole('太白',10,200)# panky = Gamerole('金莲',20,100)# pillow = Weapon('枕头',2)
# 给人物装备武器# barry.equip_weapon(pillow)# # print(barry.__dict__) #{'name': '太白', 'ad': 10, 'hp': 200, 'wea': <__main__.Weapon object at 0x000000000293E240>}# barry.wea.weapon_attack(barry,panky)
# 组合的好处:# 1,代码更合理。# 2, 类与类之间的耦合性增强。
# 05 面向对象三大特征:继承
# 继承
# class Aniaml:# def __init__(self,name,sex,age):# self.name = name# self.age = age# self.sex = sex## class Person:# def __init__(self,name,sex,age):# self.name = name# self.age = age# self.sex = sex## class Cat:# def __init__(self,name,sex,age):# self.name = name# self.age = age# self.sex = sex
# class Dog:# def __init__(self,name,sex,age):# self.name = name# self.age = age# self.sex = sex# ------------------## class Aniaml(object):# type_name = '动物类'# def __init__(self,name,sex,age):# self.name = name# self.age = age# self.sex = sex# def eat(self):# print(self)# print('吃东西')
# class Person(Aniaml):# type_name = '人类'# pass
# class Cat(Aniaml):# type_name = '猫'# pass
# class Dog(Aniaml):# type_name = '狗'# pass
# Aminal 叫做父类。# Person Cat Dog: 子类,派生类。# 单继承:# 类名:# 可以调用父类的属性,方法。# print(Person.type_name) #人类 找不到才会往父级寻找# Person.eat(111) #111 吃东西# print(Person.type_name)
# 对象# 实例化对象# p1 = Person('春哥','男',18)# p1 = Person() #报错# 对象执行类的父类的属性,方法。# print(p1.type_name) #人类# p1.type_name = '666' #修改# print(p1.type_name) #666# print(p1) #<__main__.Person object at 0x00000000026DE240># p1.eat() #<__main__.Person object at 0x00000000026DE240>
# 继承:单继承,多继承。# python 类:# 经典类:只存在python2x,不继承object的类,所以python2x既有经典类,又有新式类。# 新式类:继承object类。python3x所有的类默认都继承object。# 单继承都一样。# 多继承不一样:# 经典类 :深度优先。# 新式类 : mro 继承顺序,C3算法。
# class Animal(object):# type_name = '动物类'# def __init__(self,name,sex,age):# self.name = name# self.sex = sex# self.age = age
# def eat(self): # print(self) # print('吃东西')
# class Person(Animal):# def __init__(self,name,sex,age,mind): #mind位置可以在前面,乱序 ''' self = p1 name = '春哥' sex = 'laddboy' age = 18 mind = '有思想' ''' # Animal.__init__(self,name,sex,age) # 方法一 # super(Person,self).__init__(name,sex,age) # 方法二(简写如下) # super().__init__(name, sex, age) # 方法二 # self.mind = mind
# def eat(self): # super().eat() # print('%s 吃饭' % self.name)
# 既要执行子类方法,又要执行父类方法# 方法一: Aniaml.__init__(self,name,sex,age)# p1 = Person('春哥','laddboy',18,'有思想')# print(p1.__dict__)
# 方法二:super# p1 = Person('春哥','laddboy',18,'有思想')# print(p1.__dict__)# def func(self):# pass# self = 3# func(self)
# p1 = Person('春哥','laddboy',18,'有思想')# p1.eat()
# 继承:# 1,类与类之间的耦合性增强。# 2,节省代码。# 3,代码更加规范化,合理性。
day19_python_1124的更多相关文章
随机推荐
- Oracle数据库基础入门《二》Oracle内存结构
Oracle数据库基础入门<二>Oracle内存结构 Oracle 的内存由系统全局区(System Global Area,简称 SGA)和程序全局区(Program Global Ar ...
- Eclipse Error Reporting Welcome to the Eclipse Error Reporting Service.Do you want to help Eclipse? Enable Disable
在开发的时候,使用Eclipse IDE,提示如下信息, 这是Eclipse的错误报告,如果不想发送,可以关闭掉,关闭方法: 选择Preferences -> General -> Err ...
- Ngnix 配置文件
配置文件路径/usr/local/nginx/conf/nginx.conf user www www; #nginx 服务的伪用户和用户组 worker_processes auto; #启动进程, ...
- supervisor 守护进程
一.supervisor 安装 1.yum -y install epel-release 2.yum -y install supervisor 二.supervisor 配置文件详解 三.supe ...
- js语言精粹
1.typeof null == “object” ,所以不能通过typeof ~ == "object",判断为对象 : a.判断为null的,直接~ === null:b. ...
- <转>jmeter(十九)HTTP属性管理器
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
- 处理centos6或者7依赖关系(Nginx、MySQL、PHP)的方法
yum -y install make gcc gcc-c++ gcc-g77 flex bison file libtool libtool-libs autoconf kernel-devel ...
- hisicv200 exfat支持
由于项目中需要128Gsd卡支持.所以内核里面需要支持exfat 1.exfat 由于版权问题,所以linux kernel一直都没法支持,由于某些公司在linux kernel 3.9版本开源exf ...
- [C++ Primer Plus] 零散知识点(一)、输入函数(cin,cin.get,cin.getline等)+string头文件辨析
本文几乎照搬http://www.cnblogs.com/luolizhi/p/5746775.html博客,只修改了一点点.不知道怎么转发过来,尴尬... 学C++的时候,这几个输入函数弄的有点迷糊 ...
- 使用python玩跳一跳亲测使用步骤详解
玩微信跳一跳,测测python跳一跳,顺便蹭一蹭热度: 参考博文 使用python玩跳一跳超详细使用教程 WIN10系统,安卓用户请直入此: python辅助作者github账号为:wangshub. ...