python复习100天

python复习第12天:面向对象编程(下)

2020-04-06  本文已影响0人  潮办公

title: python复习第12天:面向对象编程(下)
date: 2020-04-04 21:00:24
tags:
- python
- 基础
categories: python复习
top: 13


面向对象编程(下)

实例方法与静态方法

class Person(object):

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

    def talk(self):
        print('我是{}, 我今年多大了'.format(self.name, self.age))  # 函数体用了self,所以这个应该定义为实例方法

    @staticmethod  # 定义为静态函数,即不需要写self参数
    def eat():
        print('我在吃饭')


p = Person('小明', 18)
p.talk()
p.eat()
"""
我是小明, 我今年多大了
我在吃饭
"""

类相关函数

class A:
    pass


class B(A):
    pass


print(issubclass(B, A))
# 输出结果
"""
True
"""
class A:
    pass


a = A()
print(isinstance(a, A))
# 输出结果
"""
True
"""
class A:
    name = "NoName"
    pass


a = A()
print(hasattr(a, 'name'))
print(hasattr(A, 'name'))
# 输出结果
"""
True
"""
class Person(object):
    def __init__(self, name):
        self.name = name


p = Person('小明')
n = getattr(p, 'name')  # 获取实例a的name属性
print(n)
"""
小明
"""
class Person(object):
    def __init__(self, name):
        self.name = name


p = Person('小明')
setattr(p, 'name', '小花')  # 获取实例a的name属性,并且赋值小花
print(p.name)
"""
小花
"""
class Person(object):
    def __init__(self, name):
        self.name = name

    def eat(self):
        print('我在吃饭')


p = Person('小明')
delattr(p, 'name')
print(p.name)
"""
AttributeError: 'Person' object has no attribute 'name'
"""
class Person(object):
    def __init__(self, name):
        self.name = name

    def eat(self):
        print('我在吃饭')


p = Person('小明')
print(dir(p))
print(dir(Person))
"""
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
 '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', 
 '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
  '__weakref__', 'eat', 'name']
  
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
 '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', 
 '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
 '__weakref__', 'eat']
"""

类的内置属性

class Person(object):
    def __init__(self, name):
        self.name = name


p = Person('小明')
print(p.__class__)
"""
<class '__main__.Person'>
"""
class Person(object):
    def __init__(self, name):
        self.name = name

    def eat(self):
        pass

p = Person('小明')
print(p.__dict__)
print(Person.__dict__)
"""
{'name': '小明'}
{'__module__': '__main__', '__init__': <function Person.__init__ at 0x7f972e5ad3b0>, 
'eat': <function Person.eat at 0x7f10febf2ef0>
'__dict__': <attribute '__dict__' of 'Person' objects>, '__weakref__':
 <attribute '__weakref__' of 'Person' objects>, '__doc__': None}
"""

__doc__:查看类的说明文档

class Person(object):
    """
    我是说明文档
    """

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

    def eat(self):
        pass

p = Person('小明')
print(p.__doc__)
"""

    我是说明文档
    

"""

类的魔法方法

操作类

class Person:
    pass
    
    def __call__(self):
        """
        当把实例化对象当成函数的时候自动调用
        """
        print('恭喜你被调用了')
p = Person()
p()
"""
恭喜你被调用了
"""
class A:
    pass


class B:
    def __str__(self):
        print('我被当字符串变量了')
        return '我是B类'
a = A()
b = B()
print(a)
print(b)
"""
<__main__.A object at 0x7f277339c290>
我被当字符串变量了
我是B类
"""

描述类

class Person(object):

    def eat(self):
        pass

    def __getattr__(self, name):
        print('没有这个东东,{}不存在'.format(name))


p = Person()
getattr(p, 'name')
"""
没有这个东东,name不存在
"""
class Person(object):

    def eat(self):
        pass

    def __len__(self):
        return 0  # 无论长度是多少,我都让它计算为零


p = Person()
print(len(p))
"""
0
"""
class Person(object):
    def __init__(self, name):
        self.name = name

    def __del__(self):
        print('有一个元素被删除了')


p = Person('小明')
del p.name
"""
有一个元素被删除了
"""

数学运算类

class Person(object):
    def __init__(self, name):
        self.name = name

    def __add__(self, other):
        return self.name + other.name


p = Person('小明')
p2 = Person('小花')
print(p + p2)
"""
小明小花
"""
class Person(object):
    def __init__(self, name):
        self.name = name

    def __sub__(self, other):
        return self.name + other.name


p = Person('小明')
p2 = Person('小花')
print(p - p2)
"""
小明小花
"""
class Person(object):
    def __init__(self, name):
        self.name = name

    def __mul__(self, other):
        return self.name + other.name


p = Person('小明')
p2 = Person('小花')
print(p * p2)
"""
小明小花
"""
上一篇 下一篇

猜你喜欢

热点阅读