天天看點

類的綁定方法和隐藏屬性

内容概要

  • 小練習(統計對象的個數)
  • 綁定方法
  • 非綁定方法(靜态方法)
  • 如何隐藏屬性
  • 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)
           
類的綁定方法和隐藏屬性
類的綁定方法和隐藏屬性