面向对象(OOP)

一 . 面向对象

概念 

类名:car
属性: color type
行为: 跑

面向对象 首先 要设计类
类名: 见名之之意 首字母大写 其他遵循驼峰命名法
属性 :见名之意 其他遵循驼峰命名法
行为:(方法/功能) :见名之意 其他遵循驼峰命名法

创建类:
类:一种数据类型 本身并不占空间 根据所学过的 number string boolean 等类似
用类创建实例化(变量)
对象占内存空间

格式:
class 类名 (父类列表):
属性
行为


类名:car

属性: color type

行为: 跑

面向对象 首先 要设计类

类名:
见名之之意 首字母大写 其他遵循驼峰命名法

属性 :见名之意 其他遵循驼峰命名法

行为:(方法/功能) :见名之意 其他遵循驼峰命名法

创建类:

类:一种数据类型 本身并不占空间 根据所学过的 number string boolean 等类似
用类创建实例化(变量) 对象占内存空间

格式:
class 类名 (父类列表):
属性
行为

object :基类 超类 所有类的父类
一般没有合适的父类就写object

1. 创建一个简单的类 基础  属性 方法 实例化

类名 (父类列表):
          属性
          行为


object  :基类  超类  所有类的父类
                   
  一般没有合适的父类就写object





# 创建一个简单的类
class ClassName(object):
       name=""
       age=0
       height=0
       weight=0

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数

       self 代表类的实例(某个对象)
          def run(self):
             print("run")

          
             
          def eat(self,food):
            print("eat"+food)
View Code
# 创建一个简单的类
class person(object):

  # 实例化对象
       name=""
       age=0
       height=0
       weight=0

       定义方法(定义函数)
       注意: 方法参数必须self 当第一个参数

       self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):
         print("eat"+food)
# 实例化对象
#        格式:对象名=类名(参数列表)    注意:没有参数列表小括号也不能省略

per1=person()
print(per1)

per2=person()
print(per2)
# 创建一个简单的类
class person(object):

  # 实例化对象
       name=""
       age=0
       height=0
       weight=0

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):
         print("eat"+food)

# 实例化对象
#        格式:对象名(变量名)=类名(参数列表)    注意:没有参数列表小括号也不能省略

per1=person()
print(per1)  # <__main__.person object at 0x024E37D0>
print(type(per1))  #<class '__main__.person'>
print(id(per1))   #33731888

per2=person()
print(per2) # <__main__.person object at 0x024E3870>
print(type(per1))  #<class '__main__.person'>
print(id(per2))    #33731984
# 创建一个简单的类
class person(object):

  # 实例化对象
       name=""
       age=0
       height=0
       weight=0

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数

       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):

         print("eat"+food)

       def openDoor(self,food):

         print("我已经打开了冰箱门")

       def filEle(self):

         print("我已经把大象装进了冰箱了哈哈哈")

        def closeDoor(self):

                 print("我已经关闭了冰箱门")

# 实例化对象

#        格式:对象名(变量名)=类名(参数列表)    注意:没有参数列表小括号也不能省略
per1=person()
# 鍒涘缓涓涓畝鍗曠殑绫
class person(object):

  # 瀹炰緥鍖栧璞
       name=""
       age=0
       height=0
       weight=0

       # 瀹氫箟鏂规硶(瀹氫箟鍑芥暟)
       # 娉ㄦ剰: 鏂规硶鍙傛暟蹇呴』self 褰撶涓涓弬鏁

       # self 浠h〃绫荤殑瀹炰緥(鏌愪釜瀵硅薄)
       
       def run(self):
          print("run")

       def eat(self,food):

         print("eat"+food)

       def openDoor(self,food):

         print("鎴戝凡缁忔墦寮浜嗗啺绠遍棬")

       def filEle(self):

         print("鎴戝凡缁忔妸澶ц薄瑁呰繘浜嗗啺绠变簡鍝堝搱鍝")

        def closeDoor(self):

                 print("鎴戝凡缁忓叧闂簡鍐扮闂")

# 瀹炰緥鍖栧璞

#        鏍煎紡:瀵硅薄鍚(鍙橀噺鍚)=绫诲悕(鍙傛暟鍒楄〃)    娉ㄦ剰:娌℃湁鍙傛暟鍒楄〃灏忔嫭鍙蜂篃涓嶈兘鐪佺暐


璁块棶灞炴:
      
        鏍煎紡 : 瀵硅薄鍚.灞炴у悕

         璧嬪 锛 瀵硅薄鍚.灞炴у悕 =鏂板

per1=person()
per1.name="寮犱笁"

per1.age=25

per1.height=165

per1.weight="80kg"

print(per1.name,per1.age,per1.height,per1.weight)
# 创建一个简单的类
class person(object):

  # 实例化对象
       name="哈哈"
       age=56
       height=150
       weight=78

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数

       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):

         print("eat"+food)

       def openDoor(self,food):

         print("我已经打开了冰箱门")

       def filEle(self):

         print("我已经把大象装进了冰箱了哈哈哈")

       def closeDoor(self):
         print("我已经关闭了冰箱门")

# 实例化对象

#        格式:对象名(变量名)=类名(参数列表)    注意:没有参数列表小括号也不能省略
# 访问属性:
      
#         格式 : 对象名.属性名
#          赋值 : 对象名.属性名 =新值

per1=person()
             # 如果创建的类里面的属性带有参数 就用默认参数   没有就传参数

per1.name="张三"

per1.age=25

per1.height=165

per1.weight="80kg"

print(per1.name,per1.age,per1.height,per1.weight)  #张三 25 165 80kg
# 创建一个简单的类
class person(object):

  # 实例化对象
       name=""
       age=0
       height=0
       weight=0

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):

         print("eat"+food)

       def openDoor(self):

         print("我已经打开了冰箱门")

       def filEle(self):

         print("我已经把大象装进了冰箱了哈哈哈")

       def closeDoor(self):
         print("我已经关闭了冰箱门")

# 实例化对象

#        格式:对象名(变量名)=类名(参数列表)    注意:没有参数列表小括号也不能省略
# 访问属性:      
#         格式 : 对象名.属性名
#          赋值 : 对象名.属性名 =新值

per1=person()
per1.name="张三"
per1.age=25
per1.height=165
per1.weight="80kg"

print(per1.name,per1.age,per1.height,per1.weight)  #张三 25 165 80kg

# 访问方法:
#       格式: 对象名.方法名(参数列表)

per1.openDoor()
per1.filEle()
per1.closeDoor()

# 我已经打开了冰箱门
# 我已经把大象装进了冰箱了哈哈哈
# 我已经关闭了冰箱门# 创建一个简单的类class person(object):
# 实例化对象
       name=""
       age=1000000000
       height=0
       weight=0
       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):

         print("eat--"+food)

       def openDoor(self):

         print("我已经打开了冰箱门")

       def filEle(self):

         print("我已经把大象装进了冰箱了哈哈哈")

       def closeDoor(self):
         print("我已经关闭了冰箱门")

# 实例化对象
#        格式:对象名(变量名)=类名(参数列表)    注意:没有参数列表小括号也不能省略

# 访问属性:
      
#         格式 : 对象名.属性名
#          赋值 : 对象名.属性名 =新值

per1=person()

per1.name="张三"

per1.age=25

per1.height=165

per1.weight="80kg"


print(per1.name,per1.age,per1.height,per1.weight)  #张三 25 165 80kg

# 访问方法:

#       格式: 对象名.方法名(参数列表)

per1.openDoor()
per1.filEle()
per1.closeDoor()

# 我已经打开了冰箱门
# 我已经把大象装进了冰箱了哈哈哈
# 我已经关闭了冰箱门

per1.eat("苹果") # eat--苹果 # 目前来看person创建的所有对象属性都是一样的 不符合逻辑常理 # 对象没有一模一样的 per2=person() print(per2.age) per3=person() print(per3.age) # 1000000000 # 1000000000

2 . 对象的初始状态 (构造函数)

# 创建一个简单的类
class person(object):

  # 实例化对象
       name=""
       age=1000000000
       height=0
       weight=0

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):
         print("eat--"+food)

       def __init__(self):
         print("这里是构造函数")

# 构造函数: __int__() 在使用类创建对象的时候自动调用

# 注意: 如果不显示的写出构造函数 默认会自动添加一个空的构造函数

per1=person()   # 这里是构造函数
# 创建一个简单的类
class person(object):

  # 实例化对象
       name=""
       age=1000000000
       height=0
       weight=0

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")
          
       def eat(self,food):
         print("eat--"+food)

       def __init__(self,name,age,height,weight):
         print(name,age,height,weight)
         print("这里是构造函数")


# 构造函数: __int__() 在使用类创建对象的时候自动调用
# 注意: 如果不显示的写出构造函数 默认会自动添加一个空的构造函数
# self  代表当前对象

per1=person("张三",20,170,"65kg") 


# 张三 20 170 65kg
# 这里是构造函数
# 创建一个简单的类
class person(object):

  # 实例化对象
       name=""
       age=1000000000
       height=0
       weight=0

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):
         print("eat--"+food)

       def __init__(self,name,age,height,weight):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight

# 使用构造函数创建出来的每个对象是不一样的

# 构造函数: __int__() 在使用类创建对象的时候自动调用
# 注意: 如果不显示的写出构造函数 默认会自动添加一个空的构造函数

# self  代表当前对象

per1=person("张三",20,170,"65kg") 
print(per1.name,per1.age,per1.height,per1.weight)
# 张三 20 170 65kg

per2=person("李四",2000,170000,"650000000kg")  
print(per2.name,per2.age,per2.height,per2.weight)
# 李四 2000 170000 650000000kg

 3 . self (关 键 字)

# self 代表类的实例     而非类

# # 那个对象调用的方法 name该方法中的self 就代表那个对象

# self.__class__   :代表类名

#注意self 不是python中 的关键字

# 创建一个简单的类
class person(object):
       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       def run(self):
          print("run")

       def eat(self,food):
         print("eat--"+food)

       def say(self):
         print(self.name ,self.age ,self.height ,self.weight)
        
       def play(aa):
          print("注意self不是关键字换成其他的标志符也是可以的")

       def __init__(self,name,age,height,weight):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight

# 使用构造函数创建出来的每个对象是不一样的

per1=person("李四",20,160,80)   
per1.say()    #李四 20 160 80

per2=person("张三丰",20000,1600000,80000)
per2.say()      #张三丰 20000 1600000 80000

per2.play()  #注意self不是关键字
# self 代表类的实例     而非类

# # 那个对象调用的方法 name该方法中的self 就代表那个对象

# self.__class__   :代表类名

# 创建一个简单的类
class person(object):

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数

       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):
         print("eat--"+food)

       def say(self):
         print(self.name ,self.age ,self.height ,self.weight)
         print(self.__class__)    #<class '__main__.person'>
      
       def __init__(self,name,age,height,weight):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight

# 使用构造函数创建出来的每个对象是不一样的

per1=person("李四",20,160,80)   
per1.say()    #李四 20 160 80

per2=person("张三丰",20000,1600000,80000) per2.say() #张三丰 20000 1600000 80000
# self 代表类的实例     而非类
# # 那个对象调用的方法 name该方法中的self 就代表那个对象
# self.__class__   :代表类名
# 创建一个简单的类
class person(object):
       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):
         print("eat--"+food)

       def say(self):
         print(self.name ,self.age ,self.height ,self.weight)
         print(self.__class__)    #<class '__main__.person'>
      
       def __init__(self,name,age,height,weight):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight

# 使用构造函数创建出来的每个对象是不一样的
per1=person("李四",20,160,80)   
per1.say()    #李四 20 160 80


per2=person("张三丰",20000,1600000,80000)
per2.say()      #张三丰 20000 1600000 80000

 4. 析构函数

# 析构函数(destructor) 与构造函数相反,当对象结束其生命周期时(例如对象所在的函数已调用完毕),系统自动执行析构函数。析构函数往往用来做"清理善后" 的工作
# (例如在建立对象时用new开辟了一片内存空间,delete会自动调用析构函数后释放内存)。

# 析构函数 :__del__()释放对象自动调用

# 创建一个简单的类
class person(object):

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print("run")

       def eat(self,food):
         print("eat--"+food)
         
       def __init__(self,name,age,height,weight):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight

       def__del__(self):
         print("这里是析构函数")

per=person("张三",25,300,100)
# 释放对象       就相当于删除了    就不能访问了   这是手动释放
del per

# 在函数里定义的对象会在函数结束时自动释放(删除)  可以减少内存浪费空间
def fun():
    per2=person("李四",1000,2000,30000)
fun()

5 . 重写__repr__和__str__函数

# 重写: 就是将函数重写一遍

 __str__() 在调用print 打印对象时自动调用 是给用户用的 是一个描述对象的方法
    
 __repr__()    是给机器用的在python 解释器里面直接敲对象在回车后调用次方法

注意:在没str时 且有repr 

优点: 当一个对象属性值很多 并且需要都需要打印 重写了
     __str__方法后简化了代码

# 创建一个简单的类
class person(object):
       def __init__(self,name,age,height,weight):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight
       def __str__(self):
         return "%s-%d-%d-%d" % (self.name,self.age,self.height,self.weigh)

per2=person("张三丰",200,165,65)
# print(per2.name,per2.age,per2.height,per2.weight)  #张三丰 200 165 65kg
print(per2)  #张三丰 200 165 65kg

# 创建一个简单的类
class person(object):
       def __init__(self,name,age,height,weight):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight
       def __repr__(self):
         return "%s-%d-%d-%d" % (self.name,self.age,self.height,self.weigh)

per2=person("张三丰",200,165,65)

print(per2.name,per2.age,per2.height,per2.weight)  #张三丰 200 165 65kg

print(per2)  #张三丰 200 165 65kg

 6 . 访问限制

# 创建一个简单的类
class person(object):

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数

       # self 代表类的实例(某个对象)
       def run(self):
          print("run")
       def eat(self,food):
         print("eat--"+food)
       def __init__(self,name,age,height,weight):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight

per=person("张三",25,300,100)

per.age=1000000000000000000000000

print(per.age)  #1000000000000000000000000
# 创建一个简单的类
class person(object):

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       def run(self):
          print("run")

       def eat(self,food):
         print("eat--"+food)
def __init__(self,name,age,height,weight,money): # 定义属性 self.name=name self.age=age self.height=height self.weight=weight self.__money=money per=person("张三",25,300,100,20000) per.age=1000000000000000000000000 print(per.age) #1000000000000000000000000 # 如果要让内部属性不被外部属性直接访问 在属性前加上两个下划线__ # 在python中 如果在属性前加上两个下划线那么这个属性就变成了私有属性 # 如果你想被外部访问内部属性直接加上 __ 列如这里__money print(per.__money) #在属性前面加上__不能访问内部属性 会报错 所以只要在内部访问 # 1000000000000000000000000 # 22222222元
# 析构函数(destructor) 与构造函数相反,当对象结束其生命周期时(例如对象所在的函数已调用完毕),系统自动执行析构函数。析构函数往往用来做"清理善后" 的工作
# (例如在建立对象时用new开辟了一片内存空间,delete会自动调用析构函数后释放内存)。

# 析构函数 :__del__()释放对象自动调用

# 创建一个简单的类
class person(object):
       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print(self.__money)  # 内部访问     #22222222元
def eat(self,food): print("eat--"+food)
def __init__(self,name,age,height,weight,money): # 定义属性 self.name=name self.age=age self.height=height self.weight=weight self.__money=money per=person("张三",25,300,100,"22222222元") per.age=1000000000000000000000000 print(per.age) #1000000000000000000000000 # 如果要让内部属性不被外部属性直接访问 在属性前加上两个下划线__ # # 在python中 如果在属性前加上两个下划线那么这个属性就变成了私有属性 # # 如果你想被外部访问内部属性直接加上 __ 列如这里__money # # print(per.__money) #在属性前面加上__不能访问内部属性 会报错 所以只要在内部访问 per.run()
# 析构函数(destructor) 与构造函数相反,当对象结束其生命周期时(例如对象所在的函数已调用完毕),系统自动执行析构函数。析构函数往往用来做"清理善后" 的工作
# (例如在建立对象时用new开辟了一片内存空间,delete会自动调用析构函数后释放内存)。
# 析构函数 :__del__()释放对象自动调用
# 创建一个简单的类
class person(object):

       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       def run(self):
          print(self.__money)  # 内部访问     #22222222元

       def eat(self,food):
         print("eat--"+food)
      
       def __init__(self,name,age,height,weight,money):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight
         self.__money=money

   # 通过内部的方法 修改私有属性
   # 通过自定义的方法实现对私有属性的赋值和取值

         def setMoney (self,money):
              # 数据过滤
              if money<0:
                 money=0
              self.__money=money

         def getMoney(self):
               return self.__money

per=person("张三",25,300,100,"22222222元")

# per.age=1000000000000000000000000

# print(per.age)  #1000000000000000000000000

# 如果要让内部属性不被外部属性直接访问    在属性前加上两个下划线__   
# 
# 在python中 如果在属性前加上两个下划线那么这个属性就变成了私有属性
# 如果你想被外部访问内部属性直接加上  __                   列如这里__money  
# print(per.__money)  #在属性前面加上__不能访问内部属性  会报错   所以只要在内部访问
per.run()
# 析构函数(destructor) 与构造函数相反,当对象结束其生命周期时(例如对象所在的函数已调用完毕),系统自动执行析构函数。析构函数往往用来做"清理善后" 的工作
# (例如在建立对象时用new开辟了一片内存空间,delete会自动调用析构函数后释放内存)。

# 析构函数 :__del__()释放对象自动调用

# 创建一个简单的类
class person(object):
       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       
       def run(self):
          print(self.__money)  # 内部访问     #22222222元

       def eat(self,food):
         print("eat--"+food)
     
       def __init__(self,name,age,height,weight,money):
        # 定义属性       
         self.name=name
         self.age=age
         self.height=height
         self.weight=weight
         self.__money=money
   # 通过内部的方法 修改私有属性
   # 通过自定义的方法实现对私有属性的赋值和取值
       def setMoney (self,money):
        # 数据过滤
        if money<0:
           money=0
        self.__money=money

       def getMoney(self):
          return self.__money

per=person("张三",25,300,100,"22222222元")

# per.age=1000000000000000000000000

# print(per.age)  #1000000000000000000000000

# 如果要让内部属性不被外部属性直接访问    在属性前加上两个下划线__   
# 
# 在python中 如果在属性前加上两个下划线那么这个属性就变成了私有属性
# 
# 如果你想被外部访问内部属性直接加上  __                   列如这里__money  
# 
# print(per.__money)  #在属性前面加上__不能访问内部属性  会报错   所以只要在内部访问
# per.run()  内部可以使用
per.setMoney(50)
print(per.setMoney())  #50
# 析构函数(destructor) 与构造函数相反,当对象结束其生命周期时(例如对象所在的函数已调用完毕),系统自动执行析构函数。析构函数往往用来做"清理善后" 的工作
# (例如在建立对象时用new开辟了一片内存空间,delete会自动调用析构函数后释放内存)。

# 析构函数 :__del__()释放对象自动调用

# 创建一个简单的类
class person(object):
       # 定义方法(定义函数)
       # 注意: 方法参数必须self 当第一个参数
       # self 代表类的实例(某个对象)
       def run(self):
          print(self.__money)  # 内部访问     #22222222元

       def eat(self,food):
         print("eat--"+food)
def __init__(self,name,age,height,weight,money): # 定义属性 self.name=name self.__age__=age self.height=height self.weight=weight self.__money=money # 通过内部的方法 修改私有属性 # 通过自定义的方法实现对私有属性的赋值和取值 def setMoney (self,money): # 数据过滤 if money<0: money=0 self.__money=money def getMoney(self): return self.__money per=person("张三",25,300,100,"22222222元") # per.age=1000000000000000000000000 # print(per.age) #1000000000000000000000000 # 如果要让内部属性不被外部属性直接访问 在属性前加上两个下划线__ # # 在python中 如果在属性前加上两个下划线那么这个属性就变成了私有属性 # # 如果你想被外部访问内部属性直接加上 __ 列如这里__money # print(per.__money) #在属性前面加上__不能访问内部属性 会报错 所以只要在内部访问 per.run() # 内部可以使用 # per.setMoney(50) # print(per.setMoney()) #50 # 在python中 __xxx__属于特殊变量 可以直接访问的 print(per.__age__)

 7 . 盖房作业面向对象

class Person(object):
    
    def func(self):

        print("盖房子")

per=Person()

per.func()

  8. 继承 

继承 :有两个类 A类和B类
当我们说A类继承自B类的时候 
那么A类就拥有了B类中的所有的属性和方法
object类  是所有类的父类  还可称为基类和超类
注意: 继承者称为子类 被继承自继承者称父类
继承作用 : 就简化代码  提高代码的健壮性   提高代码的安全性、
            是多肽的

这是person.py文件
class Person(object):

    def __init__(self,name ,age):
          
          self.name=name
          self.age=age
        
    def run(self):
             print("这是跑run")
    def  eat(self,food):
             print("这是吃eat"+food)

这是student.py文件
# 用student去区继承  person 
from person import  Person
class Student(Person):
    def __init__(self,name,age):      
    
        # 调用父类中的__int__
        super(Student,self).__init__(name,age)

这是worke.py文件
# 用student去区继承  person 
from person import  Person
class Worker(Person):
    def __init__(self,name,age):      
    
        # 调用父类中的__int__
        super(Worker,self).__init__(name,age)


执行文件.py
from student import Student
from worker import Worker

stu=Student("张三丰",18)
print(stu.name,stu.age)   #张三丰 18
stu.run() # 这是跑run

stl=Worker("李四",2222222)
print(stl.name,stl.age) #李四 2222222
stl.eat("水果哈哈哈哈哈哈哈哈哈哈")   #这是吃eat水果哈哈哈哈哈哈哈哈哈哈
这是person.py文件
class Person(object):

    def __init__(self,name ,age):
          self.name=name
          self.age=age

    def run(self):
             print("这是跑run")

    def  eat(self,food):
             print("这是吃eat"+food)

这是student.py文件
# 用student去区继承  person 
from person import  Person
class Student(Person):
    def __init__(self,name,age,stuId):      

    # stuId 是自己独有的属性   不是person 父类的属性
        # 调用父类中的__int__
        super(Student,self).__init__(name,age)

        self.stuId=stuId

这是person.py文件
# 用student去区继承  person 
from person import  Person
class Worker(Person):
    def __init__(self,name,age):      
    
        # 调用父类中的__int__
        super(Worker,self).__init__(name,age)

这是执行文件.py
from student import Student
from worker import Worker

stu=Student("张三丰",18,20)
print(stu.name,stu.age)   #张三丰 18

stu.run() # 这是跑run
# 子类可以有自己独有的属性
stu.stuId=1000000
print(stu.stuId)  1000000

stl=Worker("李四",2222222)
print(stl.name,stl.age) #李四 2222222
stl.eat("水果哈哈哈哈哈哈哈哈哈哈")   #这是吃eat水果哈哈哈哈哈哈哈哈哈哈
这是amimal.py文件
class Animal(object):
   def __int__(self,name)
      self.name=name
      super(Cat,self).__int__(name)
   def eat():

      print(self.name+"")
  
这是cat.py文件
from animal import Animal
class Cat(Animal):
   def __int__(self,name)

      #self.name=name
      super(Cat,self).__int__(name)
  #def eat():
  #print(self.name+"吃")
  

这是mouse.py文件
from animal import Animal
class Mouse(Animal):
   def __int__(self,name)

      #self.name=name
      super(Cat,self).__int__(name)
  #def eat():

      #print(self.name+"吃")
  
执行文件.py
# 多肽: 一种事物的多种形态  叫多肽
# 例如 动物(animal)     猫  狗  .......
# 目标:人可以喂任何动物

from mouse import Mouse

from cat import Cat
# 创建猫
tom=Cat("tom")

# 创建老鼠
jerry=Mouse("jerry")

tom.eat()

jerry.eat()
思考:如果在添加100种动物 也都有name 属性 和eat属性

9. 多继承 

这是Father.py文件
class Father(object):
    """docstring for Father"""
    def __init__(self,money):
        self.money=money

   def play(self):
          print("这是paly哈哈哈哈哈哈哈哈")

   def func(self):
          print("这是func啦啦啦啦啦啦啦啦")


这是Mother.py文件
class Mother(object):
    """docstring for Father"""
    def __init__(self,faceValue):
    
        self.faceValue=faceValue

   def eat(self):
          print("我要吃哈哈哈哈eat")

   def func(self):
          print("这事了绿绿绿绿func")

这是执行文件(Child.py文件去继承父亲类,母亲类)
多继承   这个Child  即继承了父类  和母类
from Father import Father
from Mother import Mother
class Child(Father,Mother):

    def __init__(self,money,faceValue):
     Father.__init__(self,money)
     Mother.__init__(self,faceValue)
这是Father.py文件
class Father(object):
    def __init__(self,money):
       self.money=money
    def play(self):
          print("这是paly哈哈哈哈哈哈哈哈")
    def func(self):
       print("这是func111111111111111111啦啦啦啦啦啦啦啦")


这是Mother.py文件
class Mother(object):
    """docstring for Father"""

    def __init__(self,faceValue):
      self.faceValue=faceValue

   def eat(self):
      print("我要吃哈哈哈哈eat")

   def func(self):
     print("这事了绿绿绿绿func")

这是Chid.py文件
# 多继承   这个Child  即继承了父类  和母类
from Father import Father
from Mother import Mother

class Child(Father,Mother):
    def __init__(self,money,faceValue):
     Father.__init__(self,money)
     Mother.__init__(self,faceValue)

这是执行文件例如(main.py)
from Child import Child
def main ():
  c=Child(300,100)
  print(c.money,c.faceValue)
  c.piay()
  c.eat()
    # 注意:父类中方法名相同 默认调用的是在括号中排前面的父类中方法
  c.func()
if __name__=="__main__":
  main()

   10. 多肽

这是Animal.py文件
class Animal(object):
   def __init__(self,name):
      self.name=name
   def eat(self):
      print(self.name+"")
  

这是Cat.py文件          让猫这个类去继承动物类 吃
from animal import Animal
class Cat(Animal):
   def __init__(self,name):
      #self.name=name
      super(Cat,self).__init__(name)
  #def eat():
  #print(self.name+"吃")
  
这是mouset.py文件             让老鼠这个类去继承动物类 吃
from animal import Animal
class Mouse(Animal):
   def __init__(self,name):
      #self.name=name
      super(Mouse,self).__init__(name)
  #def eat():
      #print(self.name+"吃")

 执行文件

# 多肽: 一种事物的多种形态 叫多肽

# 例如 动物(animal) 猫 狗 .......

# 目标:人可以喂任何动物
from cat import Cat
from mouse import Mouse

# 创建猫
tom=Cat("tom")

# 创建老鼠
jerry=Mouse("jerry")

tom.eat()
jerry.eat()

# 思考:如果在添加100种动物 也都有name 属性 和eat属性
# 定义一个有name属性 和eat 方法属性Animal 类 让所有的动物类继承自Animal

这是animal.py文件
# 动物类 都有名字这个属性   和吃这个方法
class Animal(object):
   def __init__(self,name):
      self.name=name
     
   def eat(self):
      print(self.name+"吃1111111111111111")
  
这是Cat.py文件
# 让定的猫这个属性去继承动这个方法
from animal import Animal
class Cat(Animal):
   def __init__(self,name):
      #self.name=name
      super(Cat,self).__init__(name)
  #def eat():
  #print(self.name+"吃")
  
这是mouse.py
# 让mouse去继承动物这个类  
from animal import Animal
class Mouse(Animal):
   def __init__(self,name):
      #self.name=name
      super(Mouse,self).__init__(name)
  #def eat():
      #print(self.name+"吃")
  
这是person.py文件
class Person (object):
    def feedCat(self,cat):
       print("给猫子食物")
       cat.eat()

    def feedMouse(self,mouse):
       print("给老鼠食物")
       mouse.eat()

这是执行文件
# 多肽: 一种事物的多种形态  叫多肽
# 例如 动物(animal)     猫  狗  .......
# 目标:定义一个人类可以喂任何动物
from cat import Cat
from mouse import Mouse
from person import Person
# 定义一个人类可以喂任何动物
tom=Cat("tom")

# 创建老鼠
jerry=Mouse("jerry")
tom.eat()
jerry.eat()
per=Person()

per.feedCat(tom)
per.feedCat(jerry)

    11. 对象属性与类属性

# 类属性 用类名来调用
# 对象属性用对象来调用

class Person(object):
   # 这里的属性实际上属于类属性(用类名来调用)
   name="张三"

   def __init__(self,name):
     # 这里是对象属性
  
     # 对象属性的优先级是高于类属性
        self.name=name

print(Person.name)     #张三
per=Person("李四")
print(per.name)     #李四

# 动态的给对象添加对象属性   只针对于当前对象生效
per.age=18
print(per.age)

# 类属性不能添加动态属性

注意  :对象属性和类属性千万不要重名    因为对象属性会屏蔽掉类属性

12. 动态给实例添加属性和方法并使用

 

from types import MethodType

# 创建一空类
class Person (object):
     
     pass


per=Person()

# 动态的添加属性 这体现了动态语言的灵活性
per.name="张师傅"
print(per.name)


# 动态的添加方法     必须引入一个模块
def say(self):

    print("动态的添加方法"+self.name)
per.speak=MethodType(say,per)
per.speak()  
from types import MethodType

# 创建一空类
class Person (object):
     
     pass

per=Person()

# 动态的添加属性 这体现了动态语言的灵活性
per.name="张师傅"
print(per.name)

# 动态的添加方法     必须引入一个模块
def say(self):

    print("动态的添加方法"+self.name)
per.speak=MethodType(say,per)
per.speak()  
from types import MethodType
# 创建一空类
class Person (object):
     
       # 想添加什么属性就写在里面
     __slots__=("name","age","speak")

per=Person()

# 动态的添加属性 这体现了动态语言的灵活性
per.name="张师傅"
print(per.name)

# 动态的添加方法     必须引入一个模块
def say(self):

    print("动态的添加方法"+self.name)
per.speak=MethodType(say,per)
per.speak()  


"""
思考: 如果我们想要 限制实例的属性怎么办
比如 只允许给对象添加name age height 特定的属性

解解方法:
      定义类的时候 定义一个特殊的属性 (__slots__)可以限制动态属性的添加
      """

per.age=222222
print(per.age)     

# 222222

13. @property

class Person (object):
   def __init__(self,age):
       #属性直接对暴露
      self.age=age

per=Person(18)

# 属性直接暴露在外面不安全


per.age=-15
print(per.age) #-15
class Person (object):
   def __init__(self,age):
       #属性直接对暴露
      self.age=age

per=Person(18)

# 属性直接暴露在外面不安全


per.age=-15
print(per.age) #-15
class Person (object):
   def __init__(self,age):
       #属性直接对暴露
       #限制访问
      self.__age=age
   def getAge(self):
     return self.__age
   def setAge(self,age):
     if age<0:
         age=0
     self.__age=age
     
per=Person(18)
# # 属性直接暴露在外面不安全
# per.age=-15
# print(per.age) #-15
per.setAge(15)
print(per.getAge())
class Person (object):

   def __init__(self,age):
       #属性直接对暴露
       #限制访问
      self.__age=age
"""
   def getAge(self):
     return self.__age


   def setAge(self,age):
     if age<0:
         age=0
     self.__age=age
"""

   # 方法名为受限制的变量名去掉双下划线
@property
def age(self):
      return self.__age

@age.setter           # @age.setter  去掉下划线 .setter 
def setAge(self,age):
     if age<0:
      age=0
     self.__age=age

per=Person(18)

# # 属性直接暴露在外面不安全

# per.setAge(15)
# print(per.getAge())

per.age=100                  #相当于调用setAge
print(per.age)   # 100      # 相当于调用getAge

14. 运算符重载

print(4+6)  #10

print("4"+"6")  #46

# 不同的类型用不同加法会有不同的解释

class Person(object):
    def __init__(self,num):
      self.num=num

    def __add__(self,other):
      return Person(self.num+other.num)

    def __str__(self):
       return "num="+str(self.num)


# 运算符重载 + - * /
     # 就是两个对象不能相加 给对象加法解释一下



per1=Person(1)

per2=Person(2)

print(per1+per2) #3

"""
Method  Overloads Call for
__init__  构造函数  X=Class()
__del__ 析构函数  对象销毁
__repr__  打印转换  print X,repr(X)
__str__ 打印转换  print X,str(X)
__call__  调用函数  X()
__getattr_  限制  X.undefine
__setattr__ 取值  X.any=value
__getitem__ 索引  X[key],For If
__setitem__ 索引  X[key]=value
__len__ 长度  len(X)
__iter__  迭代  
For In

__add__ + X+Y,X+=Y
__sub__ - X-Y,X-=Y
__mul__ * X*Y
__radd__  右加+ +X
__iadd__  +=  X+=Y
__or__  | X|Y,X|=Y
__cmp__ 比较 == X==Y,X<Y
__lt__  小于< X<Y
__eq__  等于= X=Y
"""

# sms.ihuyi.com
# APIID:C39463039

#APIKEY:b3858178a2526f42bce019605c5d1faa 

 15. 面向对象案例

分析需求
# 人开枪射击子弹  编程题
"""

人  
 类名:  人  (Person)

 属性: 枪   (gun)

 行为: 开火   (fire)

枪

  类名  Gun

  属性: bulletBOX

  行为:  shoot    

弹夹: 
    类名    bulletBOX

    属性     bulletCont


    行为:

"""


这是 bulletBox.py文件

class bulletBox(object):

   def __init__(self,count):
    
     self.bulletCount=count


这是gun.py文件
class Gun(object):
    
   def __init__(self,bulletBox):
      self.bulletBox=bulletBox    


   def shoot(self):
     if self.bulletBox.bulletCount==0:
       print("没有子弹")

     else:
       self.bulletBox.bulletCount-=1

       print("剩余子弹:%d发" % (self.bulletBox.bulletCount))


这是person.py文件
class Person(object):
  def __init__(self,gun):    
    self.gun=gun

  def fire(self):
    self.gun.shoot()
            
这是执行文件.py
# 人开枪射击子弹  编程题
from person import Person

from gun import Gun

from bulletbox import bulletBox

# 弹夹

bulletBox=bulletBox(5)

#

gun=Gun(bulletBox)

#
per=Person(gun)

per.fire()
per.fire()
per.fire()


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

posted @ 2019-01-07 15:07  Loversuperme  阅读(367)  评论(0编辑  收藏  举报