面向对象相关内置函数

@property 

在执行property后可执行setter  delter分别来实现修改和删除

property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值

配合隐藏属性可以实现一些功能

class Goods:
    __discount = 0.8   #静态属性
    def __init__(self,name,price):
        self.__name = name #原来的名字
        self.__price = price  #原价 都进行了隐藏,在外部无法调用
    @property
    def name(self):  #在外部调用实例.name时,返回的是这个函数的返回值
        return self.__name

    @name.setter
    def name(self,new_name): #当在外部修改对象属性时,不会直接修改,而是经过他
        self.__name = new_name

    @name.deleter  
    def name(self): #当在外部删除对象属性时,不会直接删除,而是经过他
        del self.__name

    @property
    def price(self):   #折后价
        return self.__price * Goods.__discount

    @price.setter
    def price(self,new_price):   #修改原价
        if type(new_price) is int:
            self.__price = new_price

a = Goods("大苹果",20)
print(a.__dict__)
a.name = "小苹果"
print(a.__dict__)
del a.name
print(a.__dict__#{'_Goods__name': '大苹果', '_Goods__price': 20}
#{'_Goods__name': '小苹果', '_Goods__price': 20}
#{'_Goods__price': 20}
property方法

@classmethod  @staticmethod

类方法和静态方法

类方法由一般由类名调用,且不需要和对象相关的操作

静态方法就是既不需要操作静态变量,又不需要和对象相关的操作,一般也是由类进行调用

class A:
    @staticmethod
    def func1(name):  #静态方法
        print(123)

    @classmethod
    def func2(cls):  # 类方法
        print(456)

    def func3(self): #普通方法
        pass
类方法和静态方法

super

继承时使用super就不必显式的写出父类名字

class Animal:
    def __init__(self,aggressivity, life_value,name):
        self.name = name  # 每一个角色都有自己的昵称;
        self.aggressivity = aggressivity  # 每一个角色都有自己的攻击力;
        self.life_value = life_value  # 每一个角色都有自己的生命值;
    def eat(self):
        self.life_value += 10

class Person(Animal):
    def __init__(self, name, aggressivity, life_value, money):
        #Animal.__init__(self, name, aggressivity, life_value) #这是经典类和新式类都能用的

        #在新式类中还有一种super()方法
        super().__init__(name, aggressivity, life_value)
        #super()在类中的时候括号里默认是子类名和self,所以__init__()里不用写self
        #super()在类外也可以用,需要传递子类名和一个子类的实例
        #super(Person,wang).eat()  == Animal.eat(wang)
        #不过super在外面是查不了对象属性的,所以不要这样用

        self.money = money    #派生属性:父类没有的属性
a = Person(1,2,3,4)
print(super(Person,a).money) #  报错
print(a.money) #  4
super函数

 object   isinstance   iscubclass

object是所有新式类的基类

issubclass 判断一个类是不是另一个类的子类,也可以判断两个类是不是相同的类

isinstance  判断一个对象是不是这个类实例化出来的

class A:pass
class B(A):pass
class C:pass
b = B()
print(isinstance(b,A))  # True
print(isinstance(b,object))  # True
print(issubclass(B,A))  # True
print(issubclass(A,B))  # False
print(issubclass(A,object))  # True
print(issubclass(B,object))  # True
print(issubclass(C,object))  # True

vars

# 在全局中,vars类似于dir 不过返回值不同
a = 1
b = 2
print(vars()) #  属性和值的键值对
#{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x0000027844442CC0>, 
#'__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'D:/视频/day28/day28/3.内置函数.py', '__cached__': None, 'a': 1, 'b': 2}
print(dir()) # 属性
#['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', 
#'__loader__', '__name__', '__package__', '__spec__', 'a', 'b']
  

#在传给他一个类或对象的时候,就和__dict__一样
class A:
   '''
    描述管理员的类
    '''
    c = 1
    d = 2
    def __init__(self,name):
        self.name = name
print(vars(A))
#{'__module__': '__main__', '__doc__': '\n    描述管理员的类\n    ', 'c': 1, 'd': 2, '__init__': <function A.__init__ at 0x000001A4C1428950>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>}
print(A.__dict__)
#{'__module__': '__main__', '__doc__': '\n    描述管理员的类\n    ', 'c': 1, 'd': 2, '__init__': <function A.__init__ at 0x000001A4C1428950>, '__dict__': <attribute '__dict__' of 'A' objects>, '__weakref__': <attribute '__weakref__' of 'A' objects>}
a = A("李铁柱")
print(vars(a))
#{'name': '李铁柱'}
print(a.__dict__)
#{'name': '李铁柱'}
vars

 

 

 

 

 

posted @ 2017-11-24 19:37  瓜田月夜  阅读(80)  评论(0)    收藏  举报