• 类的相关


    类的静态属性(实例变量)

    类的动态属性(类的功能方法)

    类变量:先找实例变量,再找类变量。不能通过实例变量修改类变量,无实例变量有类变量是个实例私有化属性的过程。

    self传入的是实例名

    构造函数:def __init(self,name,age):....

    析构函数:def __del__(self):....

    私有属性:__life ,则不能通过实例名.__life访问

    私有方法:__die() ,则不能通过实例名.__life()调用

    子类重构时必须先重写父类的参数,新式类与经典类:

    多继承是从左到右的继承父类(的构造函数)

    # class People:  # 经典类
    class People(object):  # 新式类
        def __init__(self, name, age):
            self.name = name
            self.age = age
            self.friends = []
    
        def eat(self):
            print('%s is eating' % self.name)
    
        def sleep(self):
            print('%s is sleeping ' % self.name)
    
    
    class Relation(object):
        def makefriends(self, obj):
            print('%s is making friend with %s' % (self.name, obj.name))
            self.friends.append(obj.name)
    
    
    class Man(People, Relation):
        # def __init__(self, name, age, money):
        #     People.__init__(self, name, age)  # 经典类
        #     Relation.__init__(self)  # 经典类
        #     super(Man, self).__init__(name, age) # 新式类
        #     self.money = money
    
        # def get_money(self):
        #     print('%s was born with %s money' % (self.name, self.money))
        pass
    
    
    class Women(People, Relation):
        pass
    
    
    # m1 = Man('chenronghua', 22, 10)
    m1 = Man('chenronghu', 22)
    # m1.get_money()
    w1 = Women('niuhangyang', 20)
    
    m1.makefriends(w1)
    w1.name = '三炮'
    print(m1.friends[0])
    View Code

     py2经典类的继承方式是深度优先,py2中新式类继承方式是广度优先

    py3中经典类和新式类都是广度优先来继承

    小练手

    # -*- coding: utf-8 -*-
    class School(object):
        def __init__(self, name, addr):
            self.name = name
            self.addr = addr
            self.students = []
            self.staffs = []
    
        def enroll_stu(self, stu_obj):
            print('给学员%s办理注册!' % stu_obj.name)
            self.students.append(stu_obj)
    
        def hire_teacher(self, tea_obj):
            self.staffs.append(tea_obj)
            print('雇佣新讲师%s' % tea_obj.name)
    
    
    class SchoolMenber(object):
        def __init__(self, name, age, sex):
            self.name = name
            self.age = age
            self.sex = sex
    
        def tell(self):
            pass
    
    
    class Teacher(SchoolMenber):
        def __init__(self, name, age, sex, salary, course):
            super(Teacher, self).__init__(name, age, sex)
            self.salary = salary
            self.course = course
    
        def tell(self):
            print('''
            ------ info of Teacher : %s -----
            Name : %s 
            Age : %s 
            Sex : %s
            Salary : %s 
            Course : %s
            ''' % (self.name, self.name, self.age, self.sex, self.salary, self.course))
    
        def teach(self):
            print('%s is teaching course [%s]' % (self.name, self.course))
    
    
    class Student(SchoolMenber):
        def __init__(self, name, age, sex, stu_id, grade):
            super(Student, self).__init__(name, age, sex)
            self.stu_id = stu_id
            self.grade = grade
    
        def tell(self):
            print('''
            -------  info of student :%s ------
            Name : %s
            Age : %s
            Sex : %s
            stu_id : %s
            grade : %s 
            ''' % (self.name, self.name, self.age, self.sex, self.stu_id, self.grade))
    
        def pay_tuition(self, money):
            print('%s is paying %s money' % (self.name, money))
    
    
    sch1 = School('old boy ', '沙河')
    t1 = Teacher('alex_py', 22, 'F', 10000, 'python')
    t2 = Teacher('alex_linux', 32, 'MF', 3000, 'linux')
    s1 = Student('chenronghua', '16', 'F', '00001', 'py')
    s2 = Student('xuliangwei', '19', 'M', '00002', 'go')
    t1.tell()
    s1.tell()
    sch1.hire_teacher(t1)
    sch1.enroll_stu(s1)
    sch1.enroll_stu(s2)
    print(sch1.students)
    print(sch1.staffs)
    sch1.staffs[0].teach()
    
    for stu in sch1.students:
        stu.pay_tuition(2000)
    View Code

    统计实例了多少个实例对象

    # -*- coding: utf-8 -*-
    class Stu(object):
        count = 0
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
            Stu.count += 1
    
    
    s1 = Stu('alex', 24)
    s2 = Stu('eric', 22)
    print(s1.count)
    print(s2.count)
    print(Stu.count)
    print(s1.__dict__)  # 没有s1自己的属性,是类的属性
    print(s2.__dict__)
    View Code

     多态性(一种接口,多种实现)

    # -*- coding: utf-8 -*-
    import abc
    
    
    class Animal(metaclass=abc.ABCMeta):  # 同一类事物:动物
        @abc.abstractmethod
        def talk(self):
            pass
    
    
    class People(Animal):  # 动物的形态之一:人
        def talk(self):
            print('say hello')
    
    
    class Dog(Animal):  # 动物的形态之二:狗
        def talk(self):
            print('say wangwang')
    
    
    class Pig(Animal):  # 动物的形态之三:猪
        def talk(self):
            print('say aoao')
    
    
    def func(Animal):
        Animal.talk()
    
    
    per1 = People()
    pig1 = Pig()
    d1 = Dog()
    func(per1)
    func(pig1)
    func(d1)
    View Code

    鸭子类型,看着像,有差不多像的属性,差不多的使用方法,那就是同一种东东

    property的使用:把某个带函数值的方法变成了类的属性

    # -*- coding: utf-8 -*-
    class People(object):
        def __init__(self, name, weight, height):
            self.name = name
            self.weight = weight
            self.height = height
    
        @property
        def bmi(self):
            return self.weight / (self.height ** 2)
    
    
    p1 = People('alex', 75, 1.81)
    print(p1.name)
    print(p1.bmi)
    View Code

     伪装属性的修改和删除

    # -*- coding: utf-8 -*-
    class People(object):
        def __init__(self, name):
            self.__name = name
    
        @property
        def name(self):
            print('getter')
            return self.__name
    
        @name.setter
        def name(self, val):
            print('setter')
            if not isinstance(val, str):
                print('名字必须是字符串类型')
                return
            self.__name = val
    
        @name.deleter
        def name(self):
            print('deleter')
            print('不允许删除!')
    View Code

    类的绑定方法与非绑定方法

    # -*- coding: utf-8 -*-
    
    '''
    在类内部定义的函数,分为两大类:
        一:绑定方法:绑定给谁,就应该由谁来调用,谁来调用就回把调用者当作第一个参数自动传入
            绑定到对象的方法:在类内定义的没有被任何装饰器修饰的
    
            绑定到类的方法:在类内定义的被装饰器classmethod修饰的方法
    
        二:非绑定方法:没有自动传值这么一说了,就类中定义的一个普通工具,对象和类都可以使用
            非绑定方法:不与类或者对象绑定
            在类的内部定义了一个跟类无关的函数
    '''
    
    
    class Foo:
        def __init__(self, name):
            self.name = name
    
        def tell(self):
            print('名字是%s' % self.name)
    
        @classmethod
        def func(cls):  # cls=Foo
            print(cls)
    
        @staticmethod
        def func1(x, y):
            print(x + y)
    
    
    f = Foo('egon')
    print(Foo.tell)
    Foo.tell(f)  # 类用这个方法的话,必须把实例名传入
    print(f.tell)
    f.tell()  # tell就是绑定到对象的方法让实例去用的
    
    # print(Foo.func)
    # Foo.func()
    
    # print(Foo.func1)
    # print(f.func1)
    View Code

     内置方法
    https://www.luffycity.com/python-book/di-5-zhang-mian-xiang-dui-xiang-bian-cheng-she-ji-yu-kai-fa/512-mian-xiang-dui-xiang-shi-zhan.html

    关于一些题目:

    https://www.cnblogs.com/huang-yc/p/9012822.html

  • 相关阅读:
    图像的轮廓发现与绘制
    原图中寻找模板匹配图像
    图像的直方图均衡化
    图像像素的重映射
    图像的采样、DOG
    将本地jar包打包到本地仓库和上传到私服
    boot+Xss防攻击的处理方案
    Springboot入门之分布式事务管理
    CRT破解版
    Springboot关于脚本脚本启动的项目:
  • 原文地址:https://www.cnblogs.com/Simonsun002/p/8934557.html
Copyright © 2020-2023  润新知