面向对象总结

一、类

  类的概念:具有相同属性和技能的一类事物

  一、静态属性

    变量:使用类名调用

# 引用静态变量
# 1.类名.__dict__['静态变量名'] 可以查看,但是不能删改
# 2.类名.静态变量名 直接就可以访问,可以删改
# 删除一个静态变量 del 类名.静态变量名
class A:role = 'sb'
print(A.__dict__)   # {'__dict__': <attribute '__dict__' of 'A' objects>, '__module__': '__main__', 'role': 'sb', '__doc__': None, '__weakref__': <attribute '__weakref__' of 'A' objects>}
print(A.__dict__['role'])   # sb
print(A.role)   # sb
del A.role
print(A.__dict__)   # {'__dict__': <attribute '__dict__' of 'A' objects>, '__module__': '__main__', '__doc__': None, '__weakref__': <attribute '__weakref__' of 'A' objects>}

  二、动态属性

    1、内置方法

      __init__

    2、普通方法

      1、自带一个self参数

      2、使用对象调用

    3、类方法

      1、@classmethod装饰的

      2、默认传cls参数

      3、使用类名可以直接调用

    4、静态方法

      1、@staticmethod装饰的

      2、没有默认参数

      3、使用类名调用,其他细节和函数相同

    5、属性方法

      1、@property装饰

        1)@funcname.setter

        2)@funcname.deleter

      2、将一个方法伪装成属性

# 引用静态变量
# 1.类名.__dict__['静态变量名'] 可以查看,但是不能删改
# 2.类名.静态变量名 直接就可以访问,可以删改
# 删除一个静态变量 del 类名.静态变量名
class A:role = 'sb'
print(A.__dict__)   # {'__dict__': <attribute '__dict__' of 'A' objects>, '__module__': '__main__', 'role': 'sb', '__doc__': None, '__weakref__': <attribute '__weakref__' of 'A' objects>}
print(A.__dict__['role'])   # sb
print(A.role)   # sb
del A.role
print(A.__dict__)   # {'__dict__': <attribute '__dict__' of 'A' objects>, '__module__': '__main__', '__doc__': None, '__weakref__': <attribute '__weakref__' of 'A' objects>}


# 引用动态变量
# 1.类名.方法名  查看这个方法的内存地址
# 1.类名.方法名(实参)  调用了这个方法,必须传一个实参,这个实参传给了self
class A:
    cat = 'bosi'
    def __init__(self,name,sex):    # 内置方法
        self.name = name
        self.sex = sex
    def action(self):   # 普通方法
        return '%s去泰国做手术,现在性别%s,现在被%s咬了' % (self.name,self.sex,self.cat)
    @classmethod
    def attack(cls,dog):    # 类方法
        cls.cat = dog
    @staticmethod   # 静态方法
    def box(father):
        return '张凯峻变成这样%s的错误' % father
person = A('张凯峻','')
ret = person.action()
print(ret)  # 张凯峻去泰国做手术,现在性别女,现在被bosi咬了
A.attack('taidi')    #
print(A.cat)    # taidi
print(A.box(''))

# 方法伪装成的属性的修改
class Goods:
    def __init__(self,name,origin_price,discount):
        self.name = name
        self.__price = origin_price
        self.__discount = discount

    @property
    def price(self):
        return self.__price * self.__discount
    @price.setter
    def price(self,new_price):
        if type(new_price) is int or type(new_price) is float:
            self.__price = new_price
apple = Goods('apple',5,0.8)
print(apple.price)  # 4.0
# 修改苹果的原价
apple.price = 8
print(apple.price)  # 6.4

# 方法伪装成的属性的删除
class Person:
    def __init__(self,n):
        self.__name = n  # 私有的属性了
    @property            # 重要程度 ****
    def name(self):
        return self.__name
    @name.deleter
    def name(self):
        print('name 被删除了')  # 只打出print结果,不是删除私有属性
    # @name.deleter         # 重要程度*
    # def name(self):
    #     del self.__name     # 执行删除

p = Person('alex')
print(p.name)   # alex
del p.name  # 只是执行了被@name.deleter装饰的函数  name 被删除了

# @property --> func     将方法伪装成属性,只观看的事儿
# @func.setter --> func  对伪装的属性进行赋值的时候调用这个方法 一般情况下用来做修改
# @func.deleter --> func 在执行del 对象.func的时候调用这个方法 一般情况下用来做删除 基本不用

# 将一些需要随着一部分属性的变化而变化的值的计算过程 从方法 伪装成属性
# 将私有的属性保护起来,让修改的部分增加一些约束,来提高程序的稳定性和数据的安全性

  三、私有的属性

二、对象

  对象的概念:就是对一个类的具体的描述

三、命名空间

四、实例化及面向对象的使用

五、面向对象三大特性

猜你喜欢

转载自www.cnblogs.com/qiujie/p/8987796.html