Disi thirteen advanced object-oriented 2

Disi thirteen advanced object-oriented 2

A class package

Package: packing, sealing up, loaded up

1. Packaging two levels

1. The first level: Objects can get stuff like, but things can get kind of objects it? Here there is a layer of encapsulating

class Foo():
    count = 0
    print(count)

f = Foo()
print(f.count)  # 0
f.name = 'nick'  # 对对象进行了封装
print(Foo.name)

2 ** Second level: internal use, external use can not be in front of you need properties plus encapsulated __ **

class People():
    __love_people = 'male'
    print('in',love_people)

    def __nawan(self):
        print('nawan')
    def __nakuaizi(self):
        print('nakuaizi')
    def __chifan(self):
        print('chifan')

    def chifan_all(self):
        self.__nawan()
        self.__nakuaizi()
        self.__chifan()

print('out',People.__f1)

p  = People()
print('out',p.nawan())
print('out',p.nakuaizi())
print('out',p.chifan())

print('out',p.chifan_all())

3. What is the use attribute this package: hiding, protecting your privacy, inside the class attributes do not want other people to access
4. What are the benefits of the method of packaging: streamlining the code, you eat on the use of this method on the line chifan , you do not need to care about other operations, and an external caller you do not know what happened inside

2. Applications

print('*'*50)
class People:
    def __init__(self,pwd):
        self.__pwd = pwd
    @property  # 方法不用加括号使用
    def pwd(self):
        return f'无法获取你的密码'

p = People('123')
print(p.pwd)
print(p.pwd)
class F1:
    __count = 0

f = F1()
print(f._F1__count)  # 如果你真的要拿,_类名__属性去拿(不可能这样做)
class Foo:
    def f1(self):
        print('FOO.f1')
    def f2(self):  # foo
        print('Foo.f2')
        self.f1()

class Bar(Foo):
    def f1(self):
        print('Bar.f1')

foo = Bar()
foo.f2() # Foo.f2  # Bar.f1
class Foo:
    def __f1(self):  # _Foo__f1
        print('FOO.f1')
    def f2(self):  # foo
        print('Foo.f2')
        self.__f1()  # _Foo__f1
print(Foo.__dict__)
class Bar(Foo):
    def __f1(self):  # _Bar__f1
        print('Bar.f1')

bar = Bar()
bar.f2() # Foo.f2
# 封装其实在定义阶段就以己经执行了,会把私有属性__f1变成_Foo__f1,之后都不会做这种处理
class Foo():
    __count = 0

foo = Foo()
print(foo)


foo.__y = 1
print(foo.__y)

Two, property characteristics such

1. BMI (Body Mass Index): Body mass index

Male body fat> 25% of women> 33% are diagnosed as obese standard

class People():

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

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

peo = People(1.8,70)
print(peo.height)
print(peo.bmi())
print(peo.bmi)

2. decorator usage (only used in Python3)

1.property generally used in: originally method, but he attributes it is time we should property

2.setattr 和 delattr

After 3.setattr and delattr use these two methods can only modify and delete properties, that is, the use of property

class People():

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

    @property  # 获取值的时候触发,你不需要加括号使用,不能加参数
    def bmi(self):
        return self.weight/(self.height**2)

    @bmi.setter  # 在修改bmi的时候触发,必须得加参数
    def bmi(self, value):
        print(f'你已经成功修改为{value}')

    @bmi.deleter  # 在删除bmi的时候触发,不能加参数
    def bmi(self):
        print('delter')

peo = People(1.8,70)
print(peo.bmi)

print('*'*50)
peo.bmi = 50

print('*'*50)
del peo.bmi

Class property usage (Python2 of)

Third, the binding method classes and objects and non-binding approach

Method binding: 1. binding method

class Foo:
    # 绑定给对象,只有对象能用,但是类也能使用,使用的时候必须得传参
    def f1(self):
        print(self)

    @classmethod  # 让被装饰的函数给类使用,约定俗称参数为cls
    # 绑定给类的方法,类能使用,对象也可以使用,但是参数依然是类
    def f2(cls):
        print(cls)

    # 什么都不绑定的,非绑定方法,定义了普通的函数
    @staticmethod
    def f3(self):
        print(self)

f = Foo()
f.f1()
Foo.f1(1111)

print('*' * 50)
Foo.f2()
f.f2()

print('*'*50)
Foo.f3(2222)
f.f3(2222)

2. When to use?

  • This method requires the use of the class as a parameter binding when you have to use the class methods, @ classmethod
class A:
    def __init__(self,name):
        self.name = name

    @classmethod
    def cla_pri(cls):
        print(f'{cls.__name__},{cls}')

class B(A):
    pass

A.cla_pri()
B.cla_pri()
x = B('x')
x.cla_pri()
'''
A,<class '__main__.A'>
B,<class '__main__.B'>
B,<class '__main__.B'>

'''
  • This method requires the use of an object as a parameter when you have to use the object binding approach
  • This method does not require class i.e. as a parameter and do not need object as a parameter, a non-binding method, @ staticmethod

IV Summary

1. Package: hidden attribute or method can not be used outside, the interior can be used in the class definition stage to perform, really would like to quote, use the class name _ __ attribute name

2. hide functions / variables within the module _x: from module import * (can not be imported), from module import _x (unreasonable)

3. @ property: the @property decorative function becomes property from a function, that function name directly, without using parentheses.

4. The method name @ .setter: @ function is modified method name .setter decoration, method name, performs the function of decoration

@bmi.setter
def set_bmi():
    print('setter')
peo.bmi = 40

5. @ method name .deleter: @ method name is deleted function .deleter decoration, method name, performs the function of this decoration

@bmi.deleter
def del_bmi():
    print('deleter')
del peo.bmi

Method in class attribute called 6.python2

Binding 7. Object: No way is to add any decorative objects bound method

8. The method of binding the class: adding a binding methods @classmethod decorator class is

The non-binding method: Add a @staticmethod decorator method is non-binding approach, in fact, an ordinary function

Guess you like

Origin www.cnblogs.com/itboy-newking/p/11072038.html