九、面向对象

一、三大编程范式

  面向过程编程

  函数式编程

  面向对象编程

    1. 类:把一类事物的相同的特征和动作整合到一起就是类

    2. 对象:基于类而创建的一个具体的事物(对象是具体存在的,也是特征和动作整合到一起的)

  

  面向对象设计:将一类具体事物的数据和动作整合到一起

  面向对象编程:用定义类+实例/对象的方法去实现面向对象设计

class School:
    def __init__(self,name,addr,type):   #定义为__init__类中不用写return,自动return self,self实际就是实例本身,是一个字典
        self.name=name
        self.addr=addr
        self.type=type
    def kaoshi(self):
        print('kaoshi')
    def zhaosheng(self):
        print('zhaosheng')

school1=School('oldboy','沙河','私立')   #生成实例,类名(参数),生成实例时,会自动调用__init__函数,将school1传给self,oldboy传给name,沙河传给addr,私立传给type
print(school1.__dict__)     #打印实例的属性字典,{'type':'私立','name':'oldboy','addr':'沙河'}
print(School.__dict__)  #打印类的属性字典

   注:实例只有数据属性,没有函数属性,但实例可以访问类的函数属性

 二、声明类

  1. 声明类

    class 类名(一般规范首字母大写):

      ‘ 类的文档字符串,注释 ’

      类体

  class People:
      country='China'
      x=1
      def __init__(obj, x, y, z): #obj=obj1,x='egon',y=18,z='male'
          obj.name = x
          obj.age = y
          obj.sex = z
      def run(self):
          print('----->', self)

  obj1=People('egon',18,'male') #People.__init__(obj1,'egon',18,'male')
  obj2=People('lxx',38,'female') #People.__init__(obj2,'lxx',38,'female')
  obj3=People('alex',38,'female') #People.__init__(obj3,'alex',38,'female')

    上面的这个__init__()叫做初始化方法(或构造方法), 在类被调用时,这个方法(虽然它是函数形式,但在类中就不叫函数了,叫方法)会自动执行,进行一些初始化的动作

  强调:
    #   1、该方法内可以有任意的python代码
    #   2、一定不能有返回值
  
  2.python2中分新式类和经典类
      经典类 class Data:
      新式类 class Data(父类名):
   python3中class Data就是一个新式类


  3. 事物的属性分为
      数据属性:即变量
      函数属性:即函数,面向对象称为方法
        类和对象都使用 . 来访问属性

class Chinese:
    dang='gongchandang'
    def aaa():
        print('aaa')
    def bbb(self):
        print('bbb')
print(Chinese.dang) #调用类的数据属性 Chinese.aaa() #调用类的函数属性 Chinese.bbb('hh') #定义bbb时没有使用任何参数,所以传参可以随便传 print(dir(Chinese)) #查看类的属性列表 print(Chinese.__dict__) #查看类的属性字典 print(Chinese.__dict__['dang']) #通过属性字典调用类的属性 Chinese.__dict__['aaa']() #通过属性字典调用类的函数属性

        类的属性:Chinese.__name__      类的名字

                 .__doc__      类的文档字符串

                 .__base__      类的第一个父类

                 .__bases__     所有父类构成的元组

                 .__dict__      类的属性

                 .__module__     类所在的模块 


class Chinese:
    dang='gongchandang'
    def __init__(self,name,age,gender):
        self.name=name
        self.age=age
        self.gender=gender
    def aaa():
        print('aaa')
    def bbb(self):
        print('bbb')
    def eat(self,food):
        print('%s吃%s'%(self.name,food))
        
p1=Chinese('',18,'female')  #生成实例,将p1传给__init__的self,张传给name,18传给age,female传给gender
print(p1.dang)   #p1的属性字典里,只有name,age,gender没有属性dang,当调用实例没有的属性时,自动向上一级在类中查找,如果类中也没有,则报错
p1.bbb()         #实例没有函数属性,但可以调用类的函数属性
p1.aaa()         #报错,实例使用.的方式调用类的函数属性时,会自动填写self参数,写成p1.aaa(self),而定义aaa时不需要参数,所以,类中的方法,都需要有一个self参数
p1.eat('')
Chinese.aaa()  #类调用函数属性时不会自动加self
Chinese.bbb()  #报错

    总结!!!:

       当调用实例没有的属性时,自动向上一级在类中查找,如果类中也没有,则报错

       实例没有函数属性,但可以调用类的函数属性

       实例使用.的方式调用类的函数属性时,会自动填写self参数,类不会自动写

       类中的方法,都需要有一个self参数

三、类的增删改查

#增加变量属性
Chinese.country='中国'    
#增加函数属性
def play(self,ball): 
    print('%s play %s'%(self.name,ball))
Chinese.play_ball=play


#删除属性
del Chinese.country


#修改变量属性
Chinese.dang='gongchandang123'
#修改函数属性
def play_ball(self,ball):
    print('%s play %s'%(self.name,ball))
Chinese.aaa=play_ball


#查看属性
Chinese.dang
Chinese.play_ball()

四、实例属性增删改查

class Chinese:
    country='china'
    def __init__(self,name):
        self.name=name
    def play_ball(self,ball):
        print('%s play %s'%(self.name,ball))

p1=Chinese('alex')    #生成实例
#print(p1.__dict__)    #查看实例属性字典{'name':'alex'}


#
#实例只能增加数据属性,没有函数属性。因为生成实例时,只是执行了__init__函数,所以只有数据属性
p1.age=18
print(p1.__dict__)  #{'name':'alex','age':'18'}


#
del p1.name
print(p1.__dict__)  #{'age':'18'}


#
p1.age=19
print(p1.__dict__)  #{'age':'19'}


#
print(p1.age)
print(p1.play_ball('pingpang'))

 五、练习~~

#例1
class Chinese:
    country='China'
    def __init__(self,name):
        self.name=name
    def play_ball(self,ball):
        print('%s play %s'%(self.name,ball))
        
p1=Chinese('alex')
print(p1.country)    #China
p1.country='China666'    #相当于增加实例的属性,不影响类的属性
print(Chinese.country)   #China
print(p1.country)   #China666


#例2
country='China'
class Chinese:
    def __init__(self,name):
        self.name=name
    def play_ball(self,ball):
        print('%s play %s'%(self.name,ball))

p1=Chinese('alex')
print(p1.country)   #报错,实例查找属性时,先在自己的属性字典中找,找不到,在类中找,再找不到则报错


#例3
country='China'
class Chinese:
    def __init__(self,name):
        self.name=name
        print('*****',country)   #此处的country只是变量名,调用类或实例的属性时,采用.的方式,如p1.country
p1=Chinese('alex')    #***** China

#例4 country='China' class Chinese: country='***China***' def __init__(self,name): self.name=name print('---->',country) print(Chinese.country) #***China*** p1=Chinese('alex') #----> China print(p1.country) #***China***


#例5
class Chinese:
    country='China'
    MMM=['a','b']
    def __init__(self,name):
        self.name=name
p1=Chinese('alex')    
print(p1.MMM)    #['a','b']
p1.MMM=[1,2,3]   #相当于给实例新增了属性MMM,不会修改类的属性
print(Chinese.MMM)   #['a','b']
print(p1.__dict__)    #{'name':'alex','MMM':[1,2,3]}


#例6
class Chinese:
    country='China'
    MMM=['a','b']
    def __init__(self,name):
        self.name=name
p1=Chinese('alex')    
print(p1.MMM)    #['a','b']  
p1.MMM.append('c')  #append不会新增属性,只会修改属性,所以修改的是类的
print(p1.__dict__)  #{'name':'alex'}
print(Chinese.MMM)    #['a','b','c']


#例7
class Chinese:
    country='China'
    MMM=['a','b']
    def __init__(self,name):
        self.name=name
p1=Chinese('alex')    
print(p1.MMM)    #['a','b']
p1.MMM=[1,2,3]
print(Chinese.MMM)   #['a','b']
print(p1.__dict__)    #{'name':'alex','MMM':[1,2,3]}
p1.MMM.append('c')  #此时p1有属性MMM,所以修改的是p1的属性
print(p1.__dict__)  #{'name':'alex''MMM':[1,2,3,'c']}
print(Chinese.MMM)    #['a','b']
原文地址:https://www.cnblogs.com/haoy/p/10476942.html