面向对象的绑定方法、非绑定方法、隐藏属性和property装饰器

面向对象的绑定方法、非绑定方法、隐藏属性和property装饰器

  • 小练习案例
  • 绑定方法
  • 非绑定方法
  • 隐藏属性
  • property装饰器

 

一、小练习案例

  1. 题目

    1)定义一个类,产生一堆对象

    2)统计产生了多少个对象

    思路:定义一个计数器,每产生一个对象,计数器加1

class Student():
    school = 'SH'
    count = 0  # 专门用来计数

    def __init__(self, name, age):
        # self => stu => {'name':'ly', 'age': 18, 'count':1}
        # self => stu1 => {'name':'ly1', 'age': 18, 'count':1}
        # self => stu2 => {'name':'ly2', 'age': 18, 'count':1}
        self.name = name
        self.age = age
        # self.count = self.count + 1
        # Student.count += 1
        # self.__class__  # Student
        self.__class__.count += 1

 

二、绑定方法

  绑定方法分为两种

  1. 绑定给对象的

class Student():
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

    # 绑定给对象的方法,对象来调用,会把自己当成第一个参数传到函数里面self
    def tell_info(self):
        print('name: %s,age:%s, gender:%s' % (self.name, self.age, self.gender))


        stu = Student('ly', 18, 'male')
        # print(stu.name)
        stu.tell_info() # stu.tell_info(stu)

  2. 绑定给类的

class Mysql():
    def __init__(self, ip, port):
        self.ip = ip
        self.port = port

    @classmethod  # 该方法绑定给类了,以后有类来调用,会自动把类名当成第一个参数传过来,cls
    def from_conf(cls):
        # cls => Oracle
        # obj = Oracle(settings.IP, settings.PORT)
        obj = cls(settings.IP, settings.PORT)
        return obj
 Mysql.from_conf()

 

三、非绑定方法

  既不绑定给类,也不绑定给对象

class Student():
    school = 'SH'
    def __init__(self, name, age):
        self.name = name
        self.age = age

    @staticmethod  # 静态方法
    def create_id():
        import uuid
        return uuid.uuid4()

stu = Student('ly', 18)

# print(stu.create_id())
# print(Student.create_id())
print(stu.create_id())

 

四、隐藏属性

  1. 隐藏的特性

    1) 在类定义阶段,发生了语法上的变形_类名__属性名

    2) 隐藏对外不对内

    3) 只有在类定义阶段发生变形,其他情况都不发生变形了

  2. 隐藏的作用

    类里面的隐藏属性, 类外部可以使用,但是目的不是让类外部使用的,类外部要是想用,在类内部开放接口进行访问

    可以达到对外部数据的严格控制

class Student():
    __school = 'SH' # _Student__school  => _类名__属性名
    def __init__(self, name, age):
        self.__name = name
        self.age = age

    def __tell_info(self):  # _Student__tell_info  => _类名__函数名
        print('name: %s, age: %s' % (self.__name, self.age))

    def get_school(self):
        return self.__school  # self._Student__school

    def set_school(self, v):
        if type(v) is not str:
            print('数据类型不合法')
        # if isinstance(v, str):
            return
        self.__school = v

 

五、property装饰器

class Student():
    __school = 'SH'  # _Student__school  => _类名__属性名

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

    def __tell_info(self):  # _Student__tell_info  => _类名__函数名
        print('name: %s, age: %s' % (self.__name, self.age))

    @property  # 把方法伪装成属性
    def name(self):
        return "name:%s" % self.__name

    @name.setter
    def name(self, v):
        if type(v) is not str:
            print('数据类型不合法')
        # if isinstance(v, str):
            return
        self.__name = v

    @name.deleter
    def name(self):
        print('不让删')

stu = Student('ly', 18)
print(stu.get_name())

stu.name = 123
print(stu.name)

del stu.name

  练习

class Bmi():
    def __init__(self, height, weight):
        self.height = height
        self.weight = weight

    @property
    def get_bmi(self):
        return self.weight / (self.height ** 2)

bmi = Bmi(1.8, 70)
print(bmi.get_bmi)

  了解

class Student():
    __school = 'SH'  # _Student__school  => _类名__属性名

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

    def __tell_info(self):  # _Student__tell_info  => _类名__函数名
        print('name: %s, age: %s' % (self.__name, self.age))

    def get_name(self):
        return "name:%s" % self.__name

    def set_name(self, v):
        if type(v) is not str:
            print('数据类型不合法')
            # if isinstance(v, str):
            return
        self.__name = v

    def del_name(self):
        print('不让删')
    
    # 了解
    name = property(get_name, set_name, del_name)


stu = Student('ly', 18)
# print(stu.xxx)

stu.name = 'aaa'
print(stu.name)

 

上一篇:FPGA系列之“Zynq MPSoC PS-PL AXI Interfaces”


下一篇:ZYNQ架构分析