天天看点

类的绑定方法和隐藏属性

内容概要

  • 小练习(统计对象的个数)
  • 绑定方法
  • 非绑定方法(静态方法)
  • 如何隐藏属性
  • property装饰器

class Counting():
    count = 0

    def __init__(self, name, age, hobby):
        self.name = name
        self.age = age
        self.hobby = hobby
        Counting.count += 1


s1 = Counting('aaa', 18, 'music')
s2 = Counting('sss', 20, 'linux')
s3 = Counting('ddd', 22, 'python')
print(s1.count)
           
类的绑定方法和隐藏属性

绑定方法分为两种
1.绑定给对象的
class Rain():
    def __init__(self, name, age, hobby):
        self.name = name
        self.age = age
        self.hobby = hobby

    def encryption(self):
        print('''
                姓名: %s
                年龄: %s
                爱好: %s
        ''' % (self.name, self.age, self.hobby))


stu = Rain('aaa', 18, 'music')
stu.encryption()
           
类的绑定方法和隐藏属性
2.绑定给类的
import settings


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

    @classmethod
    def rain(cls):
        boy = cls(settings.Name, settings.Fraction)
        return boy


res = Student.rain()
print(res.fraction)
           
类的绑定方法和隐藏属性

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

import uuid


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

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


stu = Student('aaa', 18, 'music')
print(stu.create_id())
print(Student.create_id())
           
类的绑定方法和隐藏属性

1.在定义阶段,发生了语法上的变形_类名__属性名
2.隐藏对外不对内
3.只有在类定义阶段发生变形,其他情况都不发生

为什么要隐藏:类里面的隐藏属性,类外部可以使用,但目的不是让类的外部使用,类外部想要使用可以在类的内部开放接口进行访问,可以做到对外部数据的严格控制

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

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

    def __user(self):  # _Student__user _类名__函数名
        print('''
            姓名: %s
            年龄: %s
            爱好: %s
        ''' % (self.name, self.age, self.hobby))

    # 取值
    def get(self):
        return self.__school  # self._Student__school

    # 修改
    def set(self, z):
        if not isinstance(z, str):  # 如果修改的数据不是字符串则不修改,用原来的数据
            print('数据不合法...')
            return
        self.__school = z


stu = Student('aaa', 18, 'music')
stu.set('qqq')
print(stu.get())
stu.set(777)
print(stu.get())
           
类的绑定方法和隐藏属性

class Student():
    school = 'zj'

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

    def __user(self):  # _Student__user _类名__函数名
        print('''
            姓名: %s
            年龄: %s
            爱好: %s
        ''' % (self.__name, self.age, self.hobby))

    @property  # 把方法伪装成属性
    def name(self):  # 本质还是一个函数 get_name
        return 'name: %s' % self.__name

    @name.setter  # 调用函数自动触发
    def name(self, z):  # set_name
        if not isinstance(z, str):
            print('数据不合法...')
            return
        self.__name = z

    @name.deleter
    def name(self):  # del_name
        print('无法删除...')


stu = Student('aaa', 18, 'music')
# print(stu.get())
stu.name = 'qqq'  # 把qqq传给z
print(stu.name)
stu.name = 123
print(stu.name)
del stu.name
           
类的绑定方法和隐藏属性
练习1

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

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


bmi = Bmi(1.85, 69)
print(bmi.bmi)
           
类的绑定方法和隐藏属性
练习2

class Student():
    school = 'zj'

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

    def __user(self):  # _Student__user _类名__函数名
        print('''
            姓名: %s
            年龄: %s
            爱好: %s
        ''' % (self.__name, self.age, self.hobby))

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

    def set_name(self, z):
        if not isinstance(z, str):
            print('数据不合法...')
            return
        self.__name = z

    def del_name(self):
        print('无法删除...')

    name = property(get_name, set_name, del_name)


stu = Student('sss', 18, 'music')
print(stu.name)
stu.name = 'eee'
print(stu.name)
           
类的绑定方法和隐藏属性
类的绑定方法和隐藏属性