day24 面向对象设计part1
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ------------------------------------------------------------
# 参考资料:
# 面向对象编程初步 - tonador - 博客园
# https://www.cnblogs.com/yujianbao/articles/6223482.html
#
# Python成长之路【第九篇】:Python基础之面向对象 - Mr_Albert - 博客园
# https://www.cnblogs.com/albert0924/p/8921709.html
# Python 学习 --day-16 - UMRzg - 博客园
# http://www.cnblogs.com/qinzheg/articles/9394420.html
# ------------------------------------------------------------
# ******************** day24 面向对象设计part1 *******************
# ******************** day24 面向对象设计part1 *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
# ------------------------------------------------------------
# # 1、对象概念引入
# # # 事物:狗狗; 特征:吃,叫;
# # # 一下的所有的与狗狗相关的探讨,都是要实现对所有不同狗狗的实现
# # # 下面的演示中,狗的特征与动作没有直接的关系,其他有某事物的特征也一样可以使用用该动作函数
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 2、对象概念深入探讨1
# # # 对狗的种类所具有的性质进行包装,但是具有一定的局限性,如果有多个狗的属性,每一个都要添加
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 3、对象概念深入探讨2
# # # 实现对多个属不同狗狗的属性的实现
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 4、对象概念深入探讨3
# # # 实现对多个属不同狗狗的属性的实现
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 4.1、类、对象、实例化的概念
# # #
# # # 1、什么叫类:
# # # 类是一种数据结构,就好比一个模型,该模型用来表述一类事物(事物即数据和动作的结
# # # 合体),用它来生产真实的物体(实例)
# # # 2、什么叫对象:
# # # 睁开眼,你看到的一切事物都是一个个对象,你可以把对象理解为一个具体的事物
# # #(事物即数据和动作的结合体)
# # #(铅笔是对象,人是对象,房子是对象,狗是对象,计算机是对象,水杯是对象)
# # # 3、类与对象的关系:
# # # 对象是由类产生的,上帝造人,上帝首先有一个造人的模板,这个模板即人的类,然后上帝根据类的定义来生产一个个的人
# # # 4、什么叫实例化:
# # # 由生产对象的过程叫实例化,类实例化的结果就是一个对象,或者叫做一个
# # # 实例(实例 = 对象)
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 5、对象概念深入探讨4,与序号2类似举例
# # # 实现对多个属性的函数对象函数实现
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 5.1、对象概念深入探讨5,与序号2类似举例
# # # 实现对多个属性的函数对象函数实现
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 6、类的实例化初识
# # # 具体见11
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 7、类的属性
# # # 类的数据属性, 类的函数属性(又称为方法)
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 8、类的数据属性
# # # 定义一个狗的类,然后在类中定义一个类的属性,声音是汪汪汪,这样,只要是狗,它们共有的属性就是汪汪汪
# # # 类属性又称为静态变量,或者是静态数据,这些数据是与它们所属的类对象绑定的,不依赖于任何类实例
# # # 如果你是一位Java或C++程序员,这种类型的数据相当于在一个变量声明前加上static关键字
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 8.1、类的函数属性
# # # 类的函数属性(又称为方法)
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 9、类的函数属性查看与调用
# # # 类的函数属性(又称为方法)
# # # 它的调用,类似于前面的2
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 10、特殊的类的函数属性
# # #
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 11、实例化
# # # 对比情况,见上面4
# # # 需要注意的是,类有属性: 数据 与 函数; 而实例只有数据属性,它的函数来源于类
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 12、 类属性查看
# # #
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 13、 类属性修改
# # #
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 14、 类属性 增加(类的数值属性)
# # #
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 15、 类属性 增加(类的函数属性)
# # #
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 15.1、 类属性 增加(类的函数属性)
# # #
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 16、 del, 类属性 删除
# # #
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 17、 类属性的数值属性的添加
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 18、 对实例添加数值属性与对类的关系
# # # 实例中的数值属性添加是添加到自己的地点中,而类的数值属性并没有发生变化
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 19、 类、实例的数值属性与全局变量的关系
# # # 数值属性的寻找过程: 实例-->> 调用数值属性 -->>有就返回,如果没有就继续找 -->>
# # # 类的数值属性 -->>有就返回,没有就会报错
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 19.1、 类、实例的数值属性与全局变量的关系
# # # 数值属性的寻找过程: 实例-->> 调用数值属性 -->>有就返回,如果没有就继续找 -->>
# # # 类的数值属性 -->>有就返回,没有就会报错
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 20、 对类的__init__(self, name)实现输入实例化
# # # __init__(self, name)函数是用来在类中进行实例化的,不可以在里面放置类似于有
# # # shi_li_hua()函数相关的内容,这是因是每个函数有都自己的特点的功能,不该混淆使用
# 下面的实例化是不规范的:
# def __init__(self, name):
# name = input(">>: ")
# self.name = name
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 21、实例数值属性 与 类的数值属性
# # # 实例数值属性进行赋值时,如果没有,就回去类的数值属性中查找,找到后赋值,
# # # 会将该数值属性添加到己的字典中,但是对于类中的是没有更改的,因为实例只
# # # 是调用了类的数值属性
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 21.1、实例数值属性 与 类的数值属性
# # # 实例数值属性进行赋值时,如果没有,就回去类的数值属性中查找,找到后赋值,
# # # 会将该数值属性添加到己的字典中,但是对于类中的是没有更改的,因为实例只
# # # 是调用了类的数值属性
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 22、实例数值属性 与 类的数值属性 赋值与添加的区别
# # # 如果是实例中是没有的数值属性,赋值==》添加到实例字典中
# # # 如果是实例中是没有的数值属性,添加==》添加到类 字典中
# ------------------------------------------------------------
# ------------------------------------------------分割线------------------------------------------------- # ------------------------------------------------分割线------------------------------------------------- # ------------------------------------------------分割线-------------------------------------------------
# 01 # 01 # 01 ''' # ------------------------------------------------------------ # # 1、对象概念引入 # # # 事物:狗狗; 特征:吃,叫; # # # 一下的所有的与狗狗相关的探讨,都是要实现对所有不同狗狗的实现 # # # 下面的演示中,狗的特征与动作没有直接的关系,其他有某事物的特征也一样可以使用用该动作函数 # ------------------------------------------------------------ ''' # # # 狗的特征 # dog1= { # 'name':'元英', # 'gender':"母", # 'type':'藏獒' # } # # dog2= { # 'name':'铅笔', # 'gender':"公", # 'type':'田园犬' # } # # person1= { # 'name':'tom', # 'gender':"男", # 'type':'人' # } # # 狗的动作 # def jiao(dog): # print("一条叫【%s】的狗,汪汪汪"%dog['name']) # # def chi(dog): # print("一条【%s】正在中吃东东" %dog['type']) # # # jiao(dog1) # chi(dog1) # print("分割线".center(60,"-")) # jiao(dog2) # chi(dog2) # print("分割线".center(60,"-")) # jiao(person1) # chi(person1) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # 一条叫【元英】的狗,汪汪汪 # # 一条【藏獒】正在中吃东东 # # ----------------------------分割线----------------------------- # # 一条叫【铅笔】的狗,汪汪汪 # # 一条【田园犬】正在中吃东东 # # ----------------------------分割线----------------------------- # # 一条叫【tom】的狗,汪汪汪 # # 一条【人】正在中吃东东 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 2、对象概念深入探讨1 # # # 对狗的种类所具有的性质进行包装,但是具有一定的局限性,如果有多个狗的属性,每一个都要添加 # ------------------------------------------------------------ ''' # # def dog(): # # 狗的动作 # def jiao(dog): # print("一条叫【%s】的狗,汪汪汪" %dog['name']) # # def chi(dog): # print("一条【%s】正在中吃东东" %dog['type']) # # # 狗的特征 # dog1 = { # 'name': '元英', # 'gender': "母", # 'type': '藏獒', # 'jiao': jiao, # 'chi': chi # } # return dog1 # d1 = dog() # # 返回的是一个字典 # print(d1) # d1['jiao'](d1) # d1['chi'](d1) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # {'name': '元英', 'gender': '母', 'type': '藏獒', 'jiao': <function dog.<locals>.jiao at 0x0000000002995620>, 'chi': <function dog.<locals>.chi at 0x00000000039EE7B8>} # # 一条叫【元英】的狗,汪汪汪 # # 一条【藏獒】正在中吃东东 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 3、对象概念深入探讨2 # # # 实现对多个属不同狗狗的属性的实现 # ------------------------------------------------------------ ''' # # def dog(name,gender,type): # # 狗的动作 # def jiao(dog): # print("一条叫【%s】的狗,汪汪汪" %dog['name']) # # def chi(dog): # print("一条【%s】正在中吃东东" %dog['type']) # # # 狗的特征 # dog1 = { # 'name': name, # 'gender': gender, # 'type': type, # 'jiao': jiao, # 'chi': chi # } # return dog1 # # d1 = dog( '元英',"母",'藏獒') # # 返回的是一个字典 # print(d1) # d1['jiao'](d1) # d1['chi'](d1) # print("分割线".center(60,"-")) # # d2 = dog( '铅笔', "公", '田园犬') # # 返回的是一个字典 # print(d2) # d2['jiao'](d2) # d2['chi'](d2) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # {'name': '元英', 'gender': '母', 'type': '藏獒', 'jiao': <function dog.<locals>.jiao at 0x0000000002285620>, 'chi': <function dog.<locals>.chi at 0x00000000039EE7B8>} # # 一条叫【元英】的狗,汪汪汪 # # 一条【藏獒】正在中吃东东 # # ----------------------------分割线----------------------------- # # {'name': '铅笔', 'gender': '公', 'type': '田园犬', 'jiao': <function dog.<locals>.jiao at 0x00000000039EE840>, 'chi': <function dog.<locals>.chi at 0x00000000039EE8C8>} # # 一条叫【铅笔】的狗,汪汪汪 # # 一条【田园犬】正在中吃东东 # # # # Process finished with exit code 0 # ''' # ------------------------------------------------------------ # # 4、对象概念深入探讨2 # # # 实现对多个属不同狗狗的属性的实现 # ------------------------------------------------------------ ''' # # def dog(name,gender,type): # # 狗的动作 # def jiao(dog): # print("一条叫【%s】的狗,汪汪汪" %dog['name']) # # def chi(dog): # print("一条【%s】正在中吃东东" %dog['type']) # # def init(name, gender, type): # # 狗的特征 # dog1 = { # 'name': name, # 'gender': gender, # 'type': type, # 'jiao': jiao, # 'chi': chi # } # return dog1 # return init(name, gender, type) # # d1 = dog( '元英',"母",'藏獒') # # 返回的是一个字典 # print(d1) # d1['jiao'](d1) # d1['chi'](d1) # print("分割线".center(60,"-")) # # d2 = dog( '铅笔', "公", '田园犬') # # 返回的是一个字典 # print(d2) # d2['jiao'](d2) # d2['chi'](d2) # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # {'name': '元英', 'gender': '母', 'type': '藏獒', 'jiao': <function dog.<locals>.jiao at 0x0000000002285620>, 'chi': <function dog.<locals>.chi at 0x00000000039EE7B8>} # # 一条叫【元英】的狗,汪汪汪 # # 一条【藏獒】正在中吃东东 # # ----------------------------分割线----------------------------- # # {'name': '铅笔', 'gender': '公', 'type': '田园犬', 'jiao': <function dog.<locals>.jiao at 0x00000000039EE840>, 'chi': <function dog.<locals>.chi at 0x00000000039EE8C8>} # # 一条叫【铅笔】的狗,汪汪汪 # # 一条【田园犬】正在中吃东东 # # # # Process finished with exit code 0 # # 02 面向对象设计part2 # 02 面向对象设计part2 ''' # ------------------------------------------------------------ # # 4.1、类、对象、实例化的概念 # # # # # # 1、什么叫类: # # # 类是一种数据结构,就好比一个模型,该模型用来表述一类事物(事物即数据和动作的结 # # # 合体),用它来生产真实的物体(实例) # # # 2、什么叫对象: # # # 睁开眼,你看到的一切事物都是一个个对象,你可以把对象理解为一个具体的事物 # # #(事物即数据和动作的结合体) # # #(铅笔是对象,人是对象,房子是对象,狗是对象,计算机是对象,水杯是对象) # # # 3、类与对象的关系: # # # 对象是由类产生的,上帝造人,上帝首先有一个造人的模板,这个模板即人的类,然后上帝根据类的定义来生产一个个的人 # # # 4、什么叫实例化: # # # 由生产对象的过程叫实例化,类实例化的结果就是一个对象,或者叫做一个 # # # 实例(实例 = 对象) # ------------------------------------------------------------ ''' ''' # ------------------------------------------------------------ # # 5、对象概念深入探讨4,与序号2类似举例 # # # 实现对多个属性的函数对象函数实现 # ------------------------------------------------------------''' # # def school(name, addr, type): # def init(name, addr, type): # dic_sch = { # "name": name, # "addr": addr, # "type": type, # "examination": examination, # "recruit": recruit # } # return dic_sch # # def examination(school): # print("【%s】is examing now!" %school["name"]) # def recruit(school): # print("%s【%s】 is recruiting now !" %( school["name"],school["type"]) ) # # return init(name, addr, type) # 这个是与上面2中的区别 # # s1 =school("蓝翔", "中国", "私立") # s2 =school("北大青鸟", "中国", "私立") # # s1["recruit"](s1) # s1["examination"](s1) # print("分割线".center(60,"-")) # # s2["recruit"](s2) # s2["examination"](s2) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # 蓝翔【私立】 is recruiting now ! # # 【蓝翔】is examing now! # # ----------------------------分割线----------------------------- # # 北大青鸟【私立】 is recruiting now ! # # 【北大青鸟】is examing now! # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 5.1、对象概念深入探讨5,与序号2类似举例 # # # 实现对多个属性的函数对象函数实现 # ------------------------------------------------------------''' # # def school(name, addr, type): # def init(name, addr, type): # dic_sch = { # "name": name, # "addr": addr, # "type": type, # "examination": examination, # "recruit": recruit # } # return dic_sch # # def examination(school): # print("【%s】is examing now!" %school["name"]) # def recruit(school): # print("%s【%s】 is recruiting now !" %( school["name"],school["type"]) ) # # return init(name, addr, type) # 这个是与上面2中的区别 # # s1 =school("蓝翔", "中国", "私立") # s2 =school("北大青鸟", "中国", "私立") # # s1["recruit"](s1) # s1["examination"](s1) # print("分割线".center(60,"-")) # # s2["recruit"](s2) # s2["examination"](s2) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # 蓝翔【私立】 is recruiting now ! # # 【蓝翔】is examing now! # # ----------------------------分割线----------------------------- # # 北大青鸟【私立】 is recruiting now ! # # 【北大青鸟】is examing now! # # # # Process finished with exit code 0 # # ------------------------------------------------分割线------------------------------------------------- # 03 类相关知识 # 03 类相关知识 ''' # ------------------------------------------------------------ # # 6、类的实例化初识 # # # 具体见11 # ------------------------------------------------------------ ''' # # class Chinese: # "这是一个关于中国人的类" # pass # # 实例化到底干了什么? # p1 = Chinese() # 实例化 # print(p1) # # # 实例化的过程如下 # print(p1.__init__()) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # <__main__.Chinese object at 0x0000000001E787F0> # # None # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 7、类的属性 # # # 类的数据属性, 类的函数属性(又称为方法) # ------------------------------------------------------------ ''' ''' # ------------------------------------------------------------ # # 8、类的数据属性 # # # 定义一个狗的类,然后在类中定义一个类的属性,声音是汪汪汪,这样,只要是狗,它们共有的属性就是汪汪汪 # # # 类属性又称为静态变量,或者是静态数据,这些数据是与它们所属的类对象绑定的,不依赖于任何类实例 # # # 如果你是一位Java或C++程序员,这种类型的数据相当于在一个变量声明前加上static关键字 # ------------------------------------------------------------ ''' # # class dog: # "这是一个关于狗的类" # language = "汪汪汪汪" # # print(dog.language) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # 汪汪汪汪 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 8.1、类的函数属性 # # # 类的函数属性(又称为方法) # ------------------------------------------------------------ ''' # # class dog: # "这是一个关于狗的类" # language = "汪汪汪汪" # def jiao(): # print("一条狗,汪汪汪叫" ) # # def chi(self): # print("一条狗在这里,吃东西" ) # # print(dog.language) # dog.jiao() # # # dog.chi() # 这样运行会报错,需要传入一个self的参数 # # 传入的参数是随意时,不用chi(self)中的self时,不会报错 # dog.chi("asfsfad") # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # 汪汪汪汪 # # 一条狗,汪汪汪叫 # # 一条狗在这里,吃东西 # # # # Process finished with exit code 0 # ''' # ------------------------------------------------------------ # # 9、类的函数属性查看与调用 # # # 类的函数属性(又称为方法) # # # 它的调用,类似于前面的2 # ------------------------------------------------------------ ''' # # class dog: # "这是一个关于狗的类" # language = "汪汪汪汪" # def jiao(): # print("一条狗,汪汪汪叫" ) # # def chi(self): # print("一条狗在这里,吃东西" ) # # # print("分割线===》查看类的属性".center(100,"-")) # # dir带参数时,返回参数的属性、方法列表 # print(dir(dog)) # # 查看dog类的属性,字典方式打印 # print(dog.__dict__) # # print("分割线===》查看类的调用".center(100,"-")) # print(dog.__dict__["language"]) # print(dog.__dict__["jiao"]()) # print(dog.__dict__["chi"]("saf")) # "saf"在里头并没有使用,这里随意写的 # # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # -------------------------------------------分割线===》查看类的属性-------------------------------------------- # # ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'chi', 'jiao', 'language'] # # {'__module__': '__main__', '__doc__': '这是一个关于狗的类', 'language': '汪汪汪汪', 'jiao': <function dog.jiao at 0x0000000002285620>, 'chi': <function dog.chi at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'dog' objects>, '__weakref__': <attribute '__weakref__' of 'dog' objects>} # # -------------------------------------------分割线===》查看类的调用-------------------------------------------- # # 汪汪汪汪 # # 一条狗,汪汪汪叫 # # None # # 一条狗在这里,吃东西 # # None # # # # Process finished with exit code 0 # 04 # 04 ''' # ------------------------------------------------------------ # # 10、特殊的类的函数属性 # # # # ------------------------------------------------------------ ''' # # class dog: # "这是一个关于狗的类" # language = "汪汪汪汪" # def jiao(): # print("一条狗,汪汪汪叫" ) # # def chi(self): # print("一条狗在这里,吃东西" ) # # print("__name__: ", dog.__name__) # 类的名字(字符串) # print("__doc__ ", dog.__doc__) # 类的文档字符串 # print("__base__ ", dog.__base__) # 类的第一个父类 # print("__bases__ ", dog.__bases__) # 类的所有父类构成的元组 # print("__dict__ ", dog.__dict__) # 类的属性和属性值 # print("__module__ ", dog.__module__) # 类定义所在的模块 # print("__class__ ", dog.__class__ ) # 实例dog对应的类(仅新式类中) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # __name__: dog # # __doc__ 这是一个关于狗的类 # # __bases__ <class 'object'> # # __bases__ (<class 'object'>,) # # __dict__ {'__module__': '__main__', '__doc__': '这是一个关于狗的类', 'language': '汪汪汪汪', 'jiao': <function dog.jiao at 0x0000000002965620>, 'chi': <function dog.chi at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'dog' objects>, '__weakref__': <attribute '__weakref__' of 'dog' objects>} # # __module__ __main__ # # __class__ <class 'type'> # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 11、实例化 # # # 对比情况,见上面4 # # # 需要注意的是,类有属性: 数据 与 函数; 而实例只有数据属性,它的函数来源于类 类中的初始化程序 __init__,如形式1,它的实现过程式类似于形式2的 形式1: def __init__(self, name, gender, type): print("开始运行初始化程序") self.mingzi = name self.xingbie = gender self.leixing = type print("运行完毕!!") 形式2: def __init__( name, gender, type): # 狗的特征 dog1 = { 'mingzi': name, 'xingbie': gender, 'leixing': type # 'jiao': jiao, # 'chi': chi } return dog1 # ------------------------------------------------------------ ''' # # class dog: # "这是一个关于狗的类" # language = "汪汪汪汪" # # def __init__(self, name, gender, type, age): # print("开始运行初始化程序") # print("self是: ",self) # self.mingzi = name # self.xingbie = gender # self.leixing = type # self.nianling = age # print("运行完毕!!") # # def jiao(): # print("一条狗,汪汪汪叫" ) # print("名字是%s") # # def chi(self): # print("self是: ",self) # print("一条狗在这里,吃东西" ) # print("这个吃货,年龄%s" %self.nianling) # # # d1 = dog( '元英', "母", '藏獒', 3) # 等价过程 d1 = dog.__init__(d1, '元英',"母",'藏獒', 3) # # # 返回的是一个字典 # print("d1 ",d1) # print("__dict__ ", d1.__dict__) # # print("__dict__['mingzi'] ", d1.__dict__['mingzi']) # print("mingzi ", d1.mingzi ) # # # d1.jiao()的运行会导致 TypeError: jiao() takes 0 positional arguments but 1 was given # # 原因是在国初始化中,类会默认给它传入一个参数self,但是矸在定义jiao()时,它是没有参数的 # # d1.jiao() # # # 调用实例的函数 # # 过程: 自己的字典查找 ---》没有 --》类的字典 --》找到chi() --》调用 # d1.chi() # # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # 开始运行初始化程序 # # self是: <__main__.dog object at 0x00000000029912B0> # # 运行完毕!! # # d1 <__main__.dog object at 0x00000000029912B0> # # __dict__ {'mingzi': '元英', 'xingbie': '母', 'leixing': '藏獒', 'nianling': 3} # # __dict__['mingzi'] 元英 # # mingzi 元英 # # self是: <__main__.dog object at 0x00000000029912B0> # # 一条狗在这里,吃东西 # # 这个吃货,年龄3 # # # # Process finished with exit code 0 # 05 类属性增删改查 # 05 类属性增删改查 ''' # ------------------------------------------------------------ # # 12、 类属性查看 # # # # ------------------------------------------------------------ ''' # # class Chinese: # country = "China" # def __init__(self, name): # self.name = name # # def play_ball(self,ball): # print("%s 正在打 %s" %(self.name, ball)) # # # # 查看 # print("Chinese.country: ", Chinese.country) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # Chinese.country: China # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 13、 类属性修改 # # # # ------------------------------------------------------------ ''' # # class Chinese: # country = "China" # def __init__(self, name): # self.name = name # # def play_ball(self,ball): # print("%s 正在打 %s" %(self.name, ball)) # # # # 修改 # print("修改".center(60,"-")) # Chinese.country = "Japan" # print("Chinese.country: ", Chinese.country) # p1 = Chinese("alex") # print(" p1.__dict__: ", p1.__dict__) # print("p1.country: ", p1.country) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # -----------------------------修改----------------------------- # # Chinese.country: Japan # # p1.__dict__: {'name': 'alex'} # # p1.country: Japan # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 14、 类属性 增加(类的数值属性) # # # # ------------------------------------------------------------ ''' # # class Chinese: # country = "China" # def __init__(self, name): # self.name = name # # def play_ball(self,ball): # print("%s 正在打 %s" %(self.name, ball)) # # # # # 增加(类的数值属性) # print("增加".center(60,"-")) # p1 = Chinese("alex") # Chinese.dang = "我党" # print("Chinese.dang: ", Chinese.dang) # print("p1.dang: ", p1.dang) # print(" Chinese.__dict__: ", Chinese.__dict__) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # -----------------------------增加----------------------------- # # Chinese.dang: 我党 # # p1.dang: 我党 # # Chinese.__dict__: {'__module__': '__main__', 'country': 'China', '__init__': <function Chinese.__init__ at 0x0000000002965620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None, 'dang': '我党'} # # # # Process finished with exit code 0 # # # # ''' # ------------------------------------------------------------ # # 15、 类属性 增加(类的函数属性) # # # # ------------------------------------------------------------ ''' # # class Chinese: # country = "China" # def __init__(self, name): # self.name = name # # def play_ball(self,ball): # print("%s 正在玩 %s" %(self.name, ball)) # # # p1实例化 # p1 = Chinese("alex") # # 增加(类的函数属性) 1 # print("增加".center(60,"-")) # def eat_food(self, food): # print("%s 正在吃 %s" %( self.name, food) ) # # # Chinese.eat = eat_food # print(Chinese.__dict__) # Chinese.eat(p1, "面条") # 这种用法基本不用 # print( Chinese.country) # # Chinese.play_ball(p1,"足球") # 这种用法基本不用 # p1.play_ball("足球") # p1.eat("面条") # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # {'__module__': '__main__', 'country': 'China', '__init__': <function Chinese.__init__ at 0x0000000002975620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None, 'eat': <function eat_food at 0x0000000002071EA0>} # # China # # alex 正在打 足球 # # alex 正在吃 面条 # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 15.1、 类属性 增加(类的函数属性) # # # # ------------------------------------------------------------ ''' # # # class Chinese: # country = "China" # # def __init__(self, name): # self.name = name # # def play_ball(self,ball): # print("%s 正在玩 %s" %(self.name, ball)) # # # # p1实例化 # p1 = Chinese("alex") # # # 增加(类的函数属性) 2, 下面这种用法基本不用 # # 因为把函数属性添加到了实例化当中 # print("增加".center(60,"-")) # def test(self): # print("这是一个来自实例的函数属性",self) # # 添加到p1当中 # p1.test = test # # print(p1.__dict__) # p1.test("sdaf") # # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # -----------------------------增加----------------------------- # # {'name': 'alex', 'test': <function test at 0x0000000001D21EA0>} # # 这是一个来自实例的函数属性 sdaf # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 16、 del, 类属性 删除 # # # # ------------------------------------------------------------ ''' # # class Chinese: # country = "China" # def __init__(self, name): # self.name = name # # def play_ball(self,ball): # print("%s 正在玩 %s" %(self.name, ball)) # # # Chinese.dang = "我党" # print(" Chinese.__dict__, before : ", Chinese.__dict__) # # 删除 # print("删除".center(60,"-")) # del Chinese.country # del Chinese.dang # del Chinese.play_ball # # print(" Chinese.__dict__, after : ", Chinese.__dict__) # # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # Chinese.__dict__, before : {'__module__': '__main__', 'country': 'China', '__init__': <function Chinese.__init__ at 0x0000000002985620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None, 'dang': '我党'} # # -----------------------------删除----------------------------- # # Chinese.__dict__, after : {'__module__': '__main__', '__init__': <function Chinese.__init__ at 0x0000000002985620>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} # # # # Process finished with exit code 0 # 07 # 07 ''' # ------------------------------------------------------------ # # 17、 类属性的数值属性的添加 # ------------------------------------------------------------ ''' # # class MyData: # pass # x = 10 # y = 20 # MyData.x = x # MyData.y = y # # x, y = 66, 99 # print(x,y) # print(MyData.x) # print(MyData.y) # print(MyData.__dict__) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # 66 99 # # 10 # # 20 # # {'__module__': '__main__', '__dict__': <attribute '__dict__' of 'MyData' objects>, '__weakref__': <attribute '__weakref__' of 'MyData' objects>, '__doc__': None, 'x': 10, 'y': 20} # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 18、 对实例添加数值属性与对类的关系 # # # 实例中的数值属性添加是添加到自己的地点中,而类的数值属性并没有发生变化 # ------------------------------------------------------------ ''' # # class Chinese: # country = "China" # def __init__(self, name): # self.name = name # def play_ball(self, ball): # print("%s 正在打 %s"%(self.name, ball)) # p1 = Chinese("alex") # print(p1.country) # # p1.country添加到的是p1自己实例的字典中 # p1.country = "日本" # print("类的===>>>", Chinese.country) # print("实例的==>>", p1.country) # print("Chinese.__dict__: ", Chinese.__dict__) # print("p1.__dict__: ",p1.__dict__) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # China # # 类的===>>> China # # 实例的==>> 日本 # # Chinese.__dict__: {'__module__': '__main__', 'country': 'China', '__init__': <function Chinese.__init__ at 0x0000000002995620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} # # p1.__dict__: {'name': 'alex', 'country': '日本'} # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 19、 类、实例的数值属性与全局变量的关系 # # # 数值属性的寻找过程: 实例-->> 调用数值属性 -->>有就返回,如果没有就继续找 -->> # # # 类的数值属性 -->>有就返回,没有就会报错 # ------------------------------------------------------------ ''' # # country = "中国" # class Chinese: # def __init__(self, name): # self.name = name # def play_ball(self, ball): # print(" %s 正在打 %s" %( self.name, ball)) # # # p1 = Chinese("alex") # print(p1.country) # 报错,p1并没有这个数值属性 # print(Chinese.country) # 报错,Chinese并没有这个数值属性 # # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # Traceback (most recent call last): # # File "D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py", line 980, in <module> # # print(p1.country) # # AttributeError: 'Chinese' object has no attribute 'country' # # # # Process finished with exit code 1 # ''' # ------------------------------------------------------------ # # 19.1、 类、实例的数值属性与全局变量的关系 # # # 数值属性的寻找过程: 实例-->> 调用数值属性 -->>有就返回,如果没有就继续找 -->> # # # 类的数值属性 -->>有就返回,没有就会报错 # ------------------------------------------------------------ ''' # # country = "中国" # class Chinese: # capital = "北京" # # 是一个普通的变量,可向外查找 # print("这个是类中的: " , country) # # capital,类中有定义 # print("这个是类中的: ", capital) # def __init__(self, name): # self.name = name # # country前面并有点,因此是一个普通的变量,可向外查找 # print("这个是实例中的:", country) # # 实例中的数值,自己的字典找不到,就去类的字典中找 # print("这个是实例中的: ", self.capital) # # print("这个是实例中的: ", capital) # 《报错》 # def play_ball(self, ball): # print(" %s 正在打 %s" %( self.name, ball)) # # # p1 = Chinese("alex") # print("p1.__dict__: ", p1.__dict__) # print("Chinese.__dict__: ", Chinese.__dict__) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # 这个是类中的: 中国 # # 这个是类中的: 北京 # # 这个是实例中的: 中国 # # 这个是实例中的: 北京 # # p1.__dict__: {'name': 'alex'} # # Chinese.__dict__: {'__module__': '__main__', 'capital': '北京', '__init__': <function Chinese.__init__ at 0x00000000029A5620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 20、 对类的__init__(self, name)实现输入实例化 # # # __init__(self, name)函数是用来在类中进行实例化的,不可以在里面放置类似于有 # # # shi_li_hua()函数相关的内容,这是因是每个函数有都自己的特点的功能,不该混淆使用 # 下面的实例化是不规范的: # def __init__(self, name): # name = input(">>: ") # self.name = name # ------------------------------------------------------------ ''' # # country = "中国" # class Chinese: # def __init__(self, name): # self.name = name # def play_ball(self, ball): # print(" %s 正在打 %s" %( self.name, ball)) # # def shi_li_hua(): # name = input(">>: ") # p1 = Chinese(name) # print(p1.name) # # # shi_li_hua() # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # >>: zhang # # zhang # # # # Process finished with exit code 0 # # 08 对象与实例属性补充 # 08 对象与实例属性补充 ''' # ------------------------------------------------------------ # # 21、实例数值属性 与 类的数值属性 # # # 实例数值属性进行赋值时,如果没有,就回去类的数值属性中查找,找到后赋值, # # # 会将该数值属性添加到己的字典中,但是对于类中的是没有更改的,因为实例只 # # # 是调用了类的数值属性 # ------------------------------------------------------------ ''' # # class Chinese: # country = "中国" # def __init__(self, name): # self.name = name # def play_ball(self, ball): # print(" %s 正在打 %s" %( self.name, ball)) # # # # p1 = Chinese("alex") # # print("更改前".center(100,"-") ) # print("p1.__dict__: ", p1.__dict__) # print("Chinese.__dict__: ", Chinese.__dict__) # # p1.country = "Japan" # print(Chinese.country) # # print("更改后".center(100,"-")) # print("p1.__dict__: ", p1.__dict__) # print("Chinese.__dict__: ", Chinese.__dict__) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # ------------------------------------------------更改前------------------------------------------------- # # p1.__dict__: {'name': 'alex'} # # Chinese.__dict__: {'__module__': '__main__', 'country': '中国', '__init__': <function Chinese.__init__ at 0x00000000029A5620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} # # 中国 # # ------------------------------------------------更改后------------------------------------------------- # # p1.__dict__: {'name': 'alex', 'country': 'Japan'} # # Chinese.__dict__: {'__module__': '__main__', 'country': '中国', '__init__': <function Chinese.__init__ at 0x00000000029A5620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 21.1、实例数值属性 与 类的数值属性 # # # 实例数值属性进行赋值时,如果没有,就回去类的数值属性中查找,找到后赋值, # # # 会将该数值属性添加到己的字典中,但是对于类中的是没有更改的,因为实例只 # # # 是调用了类的数值属性 # ------------------------------------------------------------ ''' # # class Chinese: # country = "中国" # l = ['a','b',123] # def __init__(self, name): # self.name = name # def play_ball(self, ball): # print(" %s 正在打 %s" %( self.name, ball)) # # # # p1 = Chinese("alex") # # print("更改前".center(100,"-") ) # print("p1.__dict__: ", p1.__dict__) # print("Chinese.__dict__: ", Chinese.__dict__) # # # 修改操作 # p1.l = [11, 22, 33, 44] # # # print("更改后".center(100,"-")) # print("p1.__dict__: ", p1.__dict__) # print("Chinese.__dict__: ", Chinese.__dict__) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # ------------------------------------------------更改前------------------------------------------------- # # p1.__dict__: {'name': 'alex'} # # Chinese.__dict__: {'__module__': '__main__', 'country': '中国', 'l': ['a', 'b', 123], '__init__': <function Chinese.__init__ at 0x00000000022C5620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} # # ------------------------------------------------更改后------------------------------------------------- # # p1.__dict__: {'name': 'alex', 'l': [11, 22, 33, 44]} # # Chinese.__dict__: {'__module__': '__main__', 'country': '中国', 'l': ['a', 'b', 123], '__init__': <function Chinese.__init__ at 0x00000000022C5620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} # # # # Process finished with exit code 0 ''' # ------------------------------------------------------------ # # 22、实例数值属性 与 类的数值属性 赋值与添加的区别 # # # 如果是实例中是没有的数值属性,赋值==》添加到实例字典中 # # # 如果是实例中是没有的数值属性,添加==》添加到类 字典中 # ------------------------------------------------------------ ''' # # class Chinese: # country = "中国" # l = ['a','b',123] # def __init__(self, name): # self.name = name # def play_ball(self, ball): # print(" %s 正在打 %s" %( self.name, ball)) # # # p1 = Chinese("alex") # p2 = Chinese("zhang") # print("更改前".center(100,"-") ) # print("p1.__dict__: ", p1.__dict__) # print("p2.__dict__: ", p2.__dict__) # # print("Chinese.__dict__: ", Chinese.__dict__) # # # 修改操作 # p1.l = [11, 22, 33, 44] # p1.l.append("====>>_____<<====") # # p2.l.append(">>>>>>>>>>>……<<<<<<<<<") # # # print("更改后".center(100,"-")) # print("p1.__dict__: ", p1.__dict__) # print("p2.__dict__: ", p2.__dict__) # print("Chinese.__dict__: ", Chinese.__dict__) # # # D:\Anaconda3\python.exe D:/C_cache/py/day24_MianXiangDuiXiangYuShiLiShuXing/day24_MianXiangDuiXiangYuShiLiShuXing.py # # ------------------------------------------------更改前------------------------------------------------- # # p1.__dict__: {'name': 'alex'} # # p2.__dict__: {'name': 'zhang'} # # Chinese.__dict__: {'__module__': '__main__', 'country': '中国', 'l': ['a', 'b', 123], '__init__': <function Chinese.__init__ at 0x0000000002975620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} # # ------------------------------------------------更改后------------------------------------------------- # # p1.__dict__: {'name': 'alex', 'l': [11, 22, 33, 44, '====>>_____<<====']} # # p2.__dict__: {'name': 'zhang'} # # Chinese.__dict__: {'__module__': '__main__', 'country': '中国', 'l': ['a', 'b', 123, '>>>>>>>>>>>……<<<<<<<<<'], '__init__': <function Chinese.__init__ at 0x0000000002975620>, 'play_ball': <function Chinese.play_ball at 0x00000000039EE7B8>, '__dict__': <attribute '__dict__' of 'Chinese' objects>, '__weakref__': <attribute '__weakref__' of 'Chinese' objects>, '__doc__': None} # # # # Process finished with exit code 0
转载请注明出处。https://www.cnblogs.com/jyfootprint/p/9457346.html