python基础10-面向对象/class类

  • 面向过程,函数式编程,面向对象,门派没有高低之分,只有练武之人有高低之分
  • 函数式编程:函数式就是用编程语言去实现数学函数。这种函数内对象是永恒不变的,要么参数是函数,要么返回值是函数,没有for和while循环,所有的循环都由递归去实现,无变量的赋值,即不用变量去保存状态,无赋值即不改变。
  • 面向对象

  • 在函数内容嵌套函数,函数有形参
  • 类,把一类事物的相同的特征和动作,也叫数据属性和函数属性整合到一起就是类,类是抽象概念。
  • 对象,就是一个基于类创建的具体事物,也是特征和动作整合到一起,人是类,姚明是对象
  • d1 = dog('袁浩','母','藏獒'),调用类dog,传入特征,生成一条叫d1的狗,面向对象设计
  • #面向对象设计
    def
    school(name,addr,type): def init(name,addr,type): sch = { 'name':name, 'addr':addr, "type":type, 'kao_shi':kao_shi,#考试的函数名 'zhao_sheng':zhao_sheng,#招生的函数名,不能执行 } return sch def kao_shi(school): print('%s 学校正在考试' %school['name']) def zhao_sheng(school): print('%s %s正在招生' %(school['type'],school['name'])) return init(name,addr,type)#在执行之前,函数已经定义即可 s1=school('oldboy','沙河','私立学校') print(s1) print(s1['name']) s1['zhao_sheng'](s1)
  • 把上述的school用类来声明,如class school,就是面向对象编程。class定义的没有return,跟函数的差别
  • 面向对象编程:使用class独特的语法,来实现面向对象设计。用def也可以,但没有class方便
  • 用面向对象语言写程序,和一个程序的设计是面向对象的,两者是八杆子打不着的两码事。c语言写的linux kernel是面向对象的程序设计,虽然c不是面向对象语言
  • python就是面向对象语言,提供了大量的class的方法应用。
  • #面向对象编程
    class
    Dog: def __init__(self,name,gender,type):#_init_是系统内置的意思,默认有return操作 self.name=name self.gender=gender self.type=type def bark(self): print('一条名字为[%s]的[%s],狂吠不止'%(self.name,self.type)) def yao_ren(self): print('[%s]正在咬人'%(self.name)) def chi_shi(self): print('[%s]正在吃屎' %(self.type)) dog1=Dog('alex','female','京巴')#实例化的过程 print(dog1.__dict__)
  • 由类生成对象的过程叫实例化,从抽象概念生成出来的具体对象
  • 实例化到底干了什么?就是调用了类中的_init_函数
  • #经典类,python3中这也是新式类
    class Chinese:
            '这是一个中国人的类'
            pass
    
    #新式类
    class Chinese(object):
           pass

    #通过.调用数据属性和函数属性,本质上就是在查_dict_属性字典
    chinese.sui_di_tu_tan()等价于这种方法,chinese._dict_[sui_di_tu_tan]()

    print(dir(Chinese))#查看类的属性
    print(chinese._dict_)#查看属性字典

    p1=Chinese('yuanhao',18,'female')#此语句实际上就是执行下边语句
    p1=chinese._init_(p1,name,age,gender)#默认把p1放到第一个参数

    #实例只有数据属性,没有函数属性。
    #p1只有数据属性
  • chinese._name_#类名
    chinese._doc_#类的描述
    chinese._base_#python所有类都有共同的祖先,叫object
    chinese._module_#显示类所在的模块,即py文件
  • 类跟函数是一样的,也有作用域的概念。实例先访问自己的属性,再访问类的属性
  • 类名()自动调用_init_函数
  • 先生成实例,再新增类的属性,然后调用实例的该属性也可以调用,说明调用实例属性的过程是引用过程,类的实时变化也可以反应到实例中
  • #给类新增函数属性
    def eat_food(self,food)
         print('%s 正在吃%s' %(self.name,food)
    
    Chinese.eat = ear.food#把函数的地址给到类的函数属性名
  • #实例属性的增删改查
    #如果给实例增加函数属性,但是不要这样干,违背了class的简洁本义
    def test(self):
         print('我是来自实例的函数属性'
    
    p1.test = test#给实例加个函数属性
    print(p1._dict_)
    
    p1.test(p1)#给实例加了函数属性,调用实例自己的函数属性,括号内要传自己,class不再给语句自动传值
  • 不要修改底层的属性字典

  • #继续实例属性的增删改查
    #
    修改 p1.age=19 print(p1._dict_) print(p1.age) #删除 del p1.age print(p1._dict_)
  • 就不信玩不蒙你

  • 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)# 实际调用类的country
    p1.country = '日本'# 在实例中新增数据属性,没有影响_init_,以后新建的实例中没有该数据属性
    print('类的-->',Chinese.country)
    print('实例的', p1.country)
    
    p2 = Chinese('yuanhao')
    print(p2.__dict__)
    print(p2.country)
  • class Chinese:
        def __init__(self):
            print('-------->?')
            name=input('请输入用户名字>>:')
            #不要这么写,init是用来生成实例的属性字典的,不应该使用输入语句
            self.name=name
    
        def play_ball(self,ball):
            print('%s 正在打%s' % (self.name,ball))
    
    p1=Chinese()
    print(p1.name) 

    #应该改为
    class Chinese:
    def __init__(self,name):
    self.name=name

    def play_ball(self,ball):
    print('%s 正在打%s' % (self.name,ball))

    name=input('请输入用户名字>>:')
    p1=Chinese(name)
    print(p1.name)
  • country='中国'
    class Chinese:
        def __init__(self,name):
            self.name=name
            print('-->',country)#只有p.这种调用方式才适用风湿理论,此处的country只是变量
    
        def play_ball(self,ball):
            print('%s 正在打%s' % (self.name,ball))
    
    p1=Chinese('alex')
  • country='中国-------'
    class Chinese:
        country='中国'
        def __init__(self,name):
            self.name=name
            print('-->',country)
            #只有p.这种调用方式才适用风湿理论,才从实例和类中找数据属性,此处的country只是变量,不从类中找
        def play_ball(self,ball):
            print('%s 正在打%s' % (self.name,ball))
    
    p1=Chinese('alex'
  • #换个姿势搞你

  • country='中国-------'
    class Chinese:
        country='中国'
        l=['q','d']
        def __init__(self,name):
            self.name=name
    
        def play_ball(self,ball):
            print('%s 正在打%s' % (self.name,ball))
    
    p1=Chinese('alex')
    print(p1.l)
    p1.l=[1,2,3]#这种是给p1赋新值,p1有了新的数据属性
    p1.l.append('c')#这种是给p1指向的l追加新值,p1本身没有l的数据属性
  • 定义类的方法
  1. 找出一类事物共有的数据属性和函数属性
  2. 把数据属性封装到init方法中
posted @ 2022-01-15 18:46  线索  阅读(55)  评论(0编辑  收藏  举报