# class Father(object):
# def run(self):
# print("Father会跑")
#
#
# class Father1(object):
# def run(self):
# print("Father1会跑")
#
#
# class Son(Father, Father1):
# pass
#
#
# s = Son()
# s.run() # Father会跑 左边优先
# class GrandFather(object):
# def sleep(self):
# print("GrandFather 睡觉")
#
#
# class Father(GrandFather):
# def run(self):
# print("Father会跑")
#
#
# class Father1(object):
# def run(self):
# print("Father1会跑")
#
# def sleep(self):
# print("Father1 睡觉")
#
#
# class Son(Father, Father1):
# pass
#
#
# s = Son()
# s.sleep() # GrandFather 睡觉 左边一条路走到底
#
class GrandFather(object):
def sleep(self):
print("GrandFather 睡觉")
class Father(GrandFather):
def run(self):
print("Father会跑")
class Father1(GrandFather):
def run(self):
print("Father1会跑")
def sleep(self):
print("Father1 睡觉")
class Son(Father, Father1):
pass
s = Son()
s.sleep() # Father1 睡觉 同一个根时,根最后执行
print(Son.__mro__) # C3算法
"""
python支持多继承
- 1.左边优先
- 2.一条道路走到底
- 3.同一个根时,根最后执行
"""
class Person(object):
def print_self(self):
print("自我介绍")
class Man(Person):
def print_self(self):
print("man的自我介绍")
def print_self(obj):
obj.print_self()
zs = Person()
# zs.print_self()
print_self(zs)
hansen = Man()
# hansen.print_self()
print_self(hansen)
# print(list().__doc__)
# li = list("abc")
# print(li.__doc__)
#
# s = "1234"
# print(s.__doc__)
# class Demo(object):
# """
# 我是注释
# """
# pass
#
#
# d = Demo()
# print(d.__doc__)
# print(Demo.__module__) # __main__ 指的是当前文件所在模块
# print(Demo.__class__) # Demo类是type的对象
# print(d.__class__) # d是Demo对象
# class Demo(object):
# country = "中国"
#
# def __init__(self):
# self.name = "deng"
# self.__age = 18
#
# def test(self):
# self.gender = "female"
# print("test")
#
#
# d = Demo()
# d.test()
# print(d.__dict__) # 字典,对象去访问,只能访问到对象中的成员(实例属性)
# print(Demo.__dict__) # 字典,类名去访问,就可以访问到类当中的成员(除了实例属性之外的属性与方法)
# print(d.__dir__()) # 列表,返回所有的成员(__dict__更倾向于是__dir__()的子集)
# class Demo(object):
# def __del__(self):
# print("我被回收了")
#
#
# d = Demo() # 1
# print("--"*10)
# print("--"*10)
# # 2 在所有代码都执行完毕之后,会自动的执行__del__方法
# class Demo(object):
# def __del__(self):
# print("我被回收了")
#
#
# d = Demo() # 1
# print("--"*10)
# del d # 2 通过关键字 del 将对象删除则会主动的执行__del__方法
# print("--"*10)
# # 3
# class Demo(object):
# def __del__(self):
# print("我被回收了")
#
#
# d = Demo() # 1
# d1 = d # 2
# print("--"*10)
# del d # 3
# del d1
# print("--"*10)
# # 4 只有当对象全部释放 才会自动触发__del__
#
# """
# 注意:python解释器会有自动的垃圾回收机制,所以,我们并不需要主动的去封装__del__()
# """
class Demo(object):
def __init__(self):
print("aaa")
def __call__(self, *args, **kwargs):
print("我可以调用了奥")
d = Demo()
d() # Demo()() TypeError: 'Demo' object is not callable
# class Demo(object):
# def __init__(self):
# print("__init__")
#
# def __new__(cls, *args, **kwargs):
# # 重写了父类的new方法,对象并没有创建成功,所以不会再触发__init__方法
# print("__new__")
#
#
# d = Demo()
# 1.__init__创建对象之后自动调用的方法
# 2.__new__用来创建对象的
# class A(object):
# def sleep(self):
# print("创建对象")
#
#
# class B(A):
# def sleep(self):
# print("我在玩")
#
#
# b = B()
# b.sleep()
class Demo(object):
def __init__(self):
print("__init__")
def __new__(cls, *args, **kwargs):
print("__new__")
# 重写父类的__new__,还需要执行父类的代码
# super().__new__(cls) # 执行了父类创建对象的代码,还并没有自动的执行__init__
return super().__new__(cls) # 将对象返回出去,自动执行__init__方法
d = Demo()
# class Single(object):
# def __new__(cls, *args, **kwargs):
# return super().__new__(cls)
#
#
# s = Single()
# s1 = Single()
# print(id(s))
# print(id(s1))
"""
当对象不存在的时候-->创建对象
当对象已经存在的时候-->永远只返回当前对象
"""
class Single(object):
__isinstance = None # 1.类属性,建立标识
def __new__(cls, *args, **kwargs):
if cls.__isinstance is None:
cls.__isinstance = super().__new__(cls) # 2.将一个对象存储到__isinstance类属性中。
# 3.__isinstance不为None,意味着它已经存储着一个对象,我们直接将返回出去即可
return cls.__isinstance
s = Single()
s1 = Single()
s2 = Single()
print(id(s)) # 1206488941624
print(id(s1)) # 1206488941624
print(id(s2))
class Gun(object):
def __init__(self,model):
self.model = model
self.bullet_count = 0
def __str__(self):
return "{}有{}发子弹".format(self.model,self.bullet_count)
def shoot(self):
if self.bullet_count > 0:
print("发射子弹")
self.bullet_count -= 1
else:
print("没有子弹啦,无法发射")
def add_bullet(self,count):
self.bullet_count += count
print("添加子弹:{}颗".format(count))
class Soldier(object):
def __init__(self,name):
self.name = name
self.qiang = 0
def fire(self):
if self.qiang == 0:
print("{}还没有抢".format(self.name))
else:
pass
# Gun().add_bullet(10)
# print("开火")
# Gun().shoot()
self.qiang.add_bullet(10)
print("开火")
self.qiang.shoot()
# 创建抢对象
AK47 = Gun("AK47")
print(AK47)
# 创建士兵对象
keen = Soldier("keen")
keen.fire()
keen.qiang = AK47
keen.fire()
print(AK47)