• Python基础第16天


                                                                       第四模块

    一:面向对象

          三大编程范式:面向过程编程,函数式编程,面向对象编程

         对象:动作与特征的结合,基于类创建的具体概念

         类:抽象概念,把一类事物的相同特性和动作整合在一起。

         面向对象编程:使用类+实例/对象方式实现面向对象设计

         实例化:由类产生对象的过程叫做实例=对象

    二:类相关知识:

    大前提:
    1.只有在python2中才分新式类和经典类,python3中统一都是新式类
    2.新式类和经典类声明的最大不同在于,所有新式类必须继承至少一个父类
    3.所有类甭管是否显式声明父类,都有一个默认继承object父类(讲继承时会讲,先记住)
    在python2中的区分
    经典类:
    class 类名:
        pass
    
    新式类:
    class 类名(父类):
        pass
    
    在python3中,上述两种定义方式全都是新式类
    
    经典类与新式类

    例子:

    def dog(name,gender,type):
        def jiao(dog):
            print('一条狗[%s],汪汪汪'%dog['name'])
        def chi_shi(dog):
            print('一条[%s]正在吃屎'%dog['type'])
        def init(name,gender,type):  #用来初始化,类
            dog1 = {
                'name':name,
                'gender':gender,
                'type':type,
                'jiao':jiao,
                'chi_shi':chi_shi
            }
            return dog1
        return init(name,gender,type)
    d1=dog('元昊','','中华田园犬')   #对象
    d2=dog('Alex','','藏獒')
    print(d1)
    print(d2)
    d1['jiao'](d1)
    d2['chi_shi'](d2)

    面向对象设计练习:

    def school(name,addr,type):
        def init(name,addr,type):
            sch = {
                'name':name,
                'addr':addr,
                'type':type,
                'exam':exam,
                'recruit':recruit
            }
            return sch
        def exam(school):
            print('[%s]正在考试'%school['name'])
        def recruit(school):
            print('[%s][%s]正在招生'%(school['name'],school['type']))
    
        return init(name,addr,type)
    s1=school('oldboy','沙河','私立学校')
    print(s1)
    s1['exam'](s1)

    可以看出,类有两种属性

    • 数据属性:变量
    • 函数属性:函数,通称方法

    notice:类和对象均用点来访问自己的属性

    •  dir(类名):查出的是个名字列表
    • 类名.__dict__:查出的是个字典
    class Chinese:
        '这是一个中国人的类'
        pass
    
    print(Chinese)
    
    p1=Chinese() #实例化过程,相当于return
    print(p1)
    class Chinese:
        '这是一个中国人的类'
        dang='gcd'
        def sui_di_tu_tan():
            print('吐痰')
    
        def cha_dui(self):
            print('插到前面看')
    print(Chinese.dang)  #数据属性
    Chinese.sui_di_tu_tan()  #函数属性
    Chinese.cha_dui('yuanhao')
    print(dir(Chinese))
    print(Chinese.__dict__)  #查看属性字典
    print(Chinese.__dict__['dang'])
    Chinese.__dict__['sui_di_tu_tan']()
    Chinese.__dict__['cha_dui'](1)

    三:对象相关知识 

         类名加括号就是实例化,会自动触发__init__函数的运行,可以用它来为每个实例定制自己的特征。对象是关于类而实际存在的一个例子,即实例

    class Chinese:
        '这是一个中国人的类'
        dang='gcd'
        # def __init__ (name,age,gender):
        #     dic={
        #         'name':name,
        #         'age':age,
        #         'gender':gender
        #     }
        #     return dic
        def __init__(self,name,age,gender):  #实例的作用域
            self.mingzi=name
            self.nianji=age
            self.xingbie=gender
        def sui_di_tu_tan(self):
            print('吐痰')
    
        def cha_dui(self):
            print('%s插到前面看'%self.mingzi)
        def eat_food(self,food):
            print('%s 正在吃  %s'%(self.mingzi,food))
    
    p1=Chinese('元昊','18','female')#实例化,本质就是调用函数--->p1=Chinese.__init__(self,name,age,gender)
    # print(p1.__dict__)
    # print(p1.__dict__['xingbie'])
    # print(p1.mingzi)
    print(p1.dang)
    # Chinese.cha_dui(p1)  #调用函数属性
    p1.sui_di_tu_tan()
    p1.eat_food('饼)’

    类属性的增删改查:

    class Chinese:
        country='China'
        def __init__(self,name):
            self.name=name
        def paly_ball(self,ball):
            print('%s 正在打 %s'%(self.name,ball))
    #查看
    print(Chinese.country)
    #修改
    Chinese.country='Japan'
    p1=Chinese('alex')
    print(p1.__dict__)
    print(p1.country)
    #增加
    Chinese.dang = 'gcd'
    print(Chinese.dang)
    print(p1.dang)
    
    #删除
    del Chinese.country
    del Chinese.dang
    print(Chinese.__dict__)
    
    def eat_food(self,food):  #顶级
        print('%s 正在吃 %s'%(self.name,food))
    
    Chinese.eat=eat_food
    print(Chinese.__dict__)
    p1.eat('shi')
    
    def test(self):
        print('test')
    Chinese.paly_ball=test()
    p1.paly_ball()

    实例属性的增删改查:

    class Chinese:
        country='China'
        def __init__(self,name):
            self.name=name
        def paly_ball(self,ball):
            print('%s 正在打 %s'%(self.name,ball))
    p1=Chinese('alex')
    print(p1.__dict__) #查看实例属性字典
    # 查看
    
    print(p1.name)
    print(p1.country)
    p1.paly_ball('篮球')
    
    # 增加
    p1.age=18
    print(p1.__dict__)
    
    # 不要修改底层的属性字典
    
    # 修改
    p1.age=19
    print(p1.__dict__)
    print(p1.age)
    
    #删除
    del p1.age
    print(p1.__dict__)
    对象与实例属性:
    class MyData:
        pass
    
    x=10
    y=20
    MyData.x=1
    MyData.y=2
    print(x,y)
    print(MyData.x,MyData.y)
    print(MyData.x+MyData.y)
    
    # 例一
    class Chinese:
        country='China'
        def __init__(self,name):
            self.name=name
        def paly_ball(self,ball):
            print('%s 正在打 %s'%(self.name,ball))
    p1=Chinese('alex')
    print(p1.country)
    p1.country='Japan'
    print(Chinese.country) #类的
    print(p1.country)  #实例的
    
    # 例二
    country='China'
    class Chinese:
        # country='China'
        def __init__(self,name):
            self.name=name
        def paly_ball(self,ball):
            print('%s 正在打 %s'%(self.name,ball))
    p1=Chinese('alex')
    print(p1.country)
    p1.country='Japan'
    print(Chinese.country) #类的
    print(p1.country)  #实例的
    
    country='China'
    class Chinese:
        # country='China'
        def __init__(self):
            print('------------>?')
            name=input('请输入荣户名》》:')
            self.name=name
        def paly_ball(self,ball):
            print('%s 正在打 %s'%(self.name,ball))
    p1=Chinese()
    print(p1.name)
    
    # 不要把输入输出写到函数中 ,所以以上写的不好
    
    country='China'
    class Chinese:
        def __init__(self,name):
            self.name=name
            print('----->',country)  #没加点则表示既不是函数属性也不是类属性,只是一个普通变量
        def paly_ball(self,ball):
            print('%s 正在打 %s'%(self.name,ball))
    p1=Chinese('alex')
    
    
    
    country='China----------------'
    class Chinese:
        country='China'
        def __init__(self,name):
            self.name=name
            print('----->',country)
        def paly_ball(self,ball):
            print('%s 正在打 %s'%(self.name,ball))
    
    print(Chinese.__dict__)
    print(Chinese.country)
    p1=Chinese('alex')
    print('实例---->',p1.country)
    
    
    
    class Chinese:
        country = 'China'
        l=['a','b']
        def __init__(self,name):
            self.name=name
        def paly_ball(self,ball):
            print('%s 正在打 %s'%(self.name,ball))
    p1=Chinese('alex')
    # print(p1.country)
    # p1.country='Japan'
    # print(Chinese.country)
    print(p1.l)
    # p1.l=[1,2,3]
    # print(Chinese.l)
    # print(p1.__dict__)
    p1.l.append('c')  #给类改
    print(p1.__dict__)
    print(Chinese.l)

      

         

  • 相关阅读:
    Xpath语法
    Centos 6.5 本地局域网基于HTTP搭建YUM
    前端接口自动化测试工具-DOClever使用介绍(转载)
    mysql指令
    vue+element+echarts饼状图+可折叠列表
    vue+element+echarts柱状图+列表
    缓冲流
    查询同一张表符合条件的某些数据的id拼接成一个字段返回
    Properties集合
    JDK7&JDK9处理异常新特性
  • 原文地址:https://www.cnblogs.com/xyd134/p/6522947.html
Copyright © 2020-2023  润新知