python自学-class17(up)-继承Obiect

1.继承

#继承的意义是代码重用,数据,函数都可以重用
#子类覆盖,子类与父类重名,子类会覆盖父类
class parent:
    def __init__(self):
        self.money=1000e6
        self.mystr="it is for your"
    def doing(self):
        print("working!!!")
    def buy(self):
        print("have maney")
class sun(parent):
    def __init__(self):
        #parent.__init__(self)  #第一种风格初始化父类
        super().__init__()    #第二种风格
        self.mystr="lazy!"
    def doing(self):
        print("play game!!")

per=parent()
per.doing()
print("parent",per.money)
print(per.doing())
suns=sun()
suns.doing()
print("sun",suns.money)
print(suns.doing())

2.多继承(要注意属性与方法的继承顺序)

class turlly:
    def __init__(self):
        self.money=10000
        self.bike="自行车"
        self.live="毛坯房"
    def buy(self):
        print("could not buy something")
    def do(self):
        print("worker")
class background:
    def __init__(self):
        self.money=20000000000
        self.car="兰博基尼"
        self.house="别墅"
    def bigbuy(self):
        print("could buy anything")
    def do(self):
        print("土豪")


class me(turlly,background):  #多继承,继承俩个类
    def __init__(self):
        turlly.__init__(self)
        background.__init__(self)
        pass
    def want(self):
        print("i want money!!!")

#多继承,同时继承俩个类的属性和方法
#
txm=me()
print(txm.money)   #属性,后来的覆盖先前
txm.buy()
txm.bigbuy()
txm.do()           #方法,前面的覆盖后面的
'''
属性与初始化顺序有关,后来覆盖前者
turlly.__init__(self)
background.__init__(self)
方法与继承顺序有关,前面覆盖后来
class me(turlly,background):
'''

3.继承的局限性

class fur:
    def __init__(self):
        self.__wife="mor"   #私有变量定义

def sun(fur):
    def __init__(self):
        super().__init__()
        #print(self.__wife)   #私有变量不可以被继承

some=sun()
#print(some.__wife)

4.object是所有类的,默认父类

class MyOBJ:
    pass
class MyOBJK(object):
    pass
#所有类都有一个共同的默认父类object
my1=MyOBJ()
print(dir(my1))
my2=MyOBJK()
print(dir(my2))

5.类的属性

class parent:
    """
    hello python
    """
    def __init__(self):
        self.money=1000e6
        self.mystr="it is for your"
    def doing(self):
        print("working!!!")
    def buy(self):
        print("have maney")

print(parent.__doc__)      #类的说明
print(parent.__name__)   #类的名称
print(parent.__module__)  #从哪个地方开始执行
print(parent.__bases__)   #类的父类
print(parent.__dict__)    #字典,包含所有属性和值

6.super减少父类构造,节约内存

'''
#多次构造浪费内存
class school:
    def __init__(self):
        print("school构造一次")
class stu1(school):
    def __init__(self):
        school.__init__(self)
        print("stu1构造一次")
class stu2(school):
    def __init__(self):
        school.__init__(self)
        print("stu2构造一次")
class stu3(school):
    def __init__(self):
        school.__init__(self)
        print("stu3构造一次")

class teacher(stu1,stu2,stu3):
    def __init__(self):
        stu1.__init__(self)
        stu2.__init__(self)
        stu3.__init__(self)
point=teacher()
'''
class school:
    def __init__(self):
        print("school构造一次")
class stu1(school):
    def __init__(self):
        super().__init__()
        print("stu1构造一次")
class stu2(school):
    def __init__(self):
        super().__init__()
        print("stu2构造一次")
class stu3(school):
    def __init__(self):
        super().__init__()
        print("stu3构造一次")

class teacher(stu1,stu2,stu3):
    def __init__(self):
        super().__init__()
#super减少父类构造,节约内存
point=teacher()

7.isinstance判断变量类型

'''
a=10
print(isinstance(a,int))  #判断变量类型
mystr="12165"
print(isinstance(a,str))
print(isinstance(mystr,str))
print(isinstance(a,(str,int)))   #(str,int)俩种类型中的一个
'''
class school:
    pass
class stu(school):
    pass
print(isinstance(school(),school))
print(isinstance(stu(),school))  #判断子类属于父类
print(type(school())==school)
print(type(stu()==school))      #type对类型严格检查,不能判断子类等于父类

8.多态解决软件可拓展性

#业务需求通过一个接口解决,一个接口连接多种形态
class sever:
    pass

class makerice(sever):
    def make(self):
        sever.__init__(self)
        print("make rice")

class makeapple(sever):
    def make(self):
        sever.__init__(self)
        print("make apple")
#统一接口
def makesever(obj):
    obj.make()
#多态解决软件可拓展性
#即实现了需求改变,但是接口make不变
m1=makeapple()
m2=makerice()
makesever(m1)
makesever(m2)

9.静态方法与常用方法

#类是对象的一种抽象
class People:
    def __init__(self,tall):
        self.tall=tall

    @staticmethod    #静态方法
    def geteyes():
        return 2
    def gettall(self):   #常用方法,需要实例化才能查看和使用
        return self.tall

print(People.geteyes())   #通用的东西可以设置成静态方法,不用实例化就可以使用
p1=People(176)   #需要实例化的常用方法
print(p1.geteyes())

10.类方法

class people:
    def __init__(self):
        self.money=1000000

    @classmethod   #标识符,可以省略,类的内部方法
    def doing(self):
        print("working")

#把类当作参数传递,类的外部方法
def getmoney(txm):
    return txm.money
txm=people()
print(getmoney(txm))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值