武汉13期吴德军 发表于 2022-4-26 19:15:25

python调用和继承

实例方法、静态方法、类方法的区别1.参数的区别实例方法:定义实例方法时最少有一个形参--实例对象,通常self类方法:定义类方法时最少有一个形参--类对象,通常用cls静态方法:定义静态方法时可以不用定义形参2.方法定义时添加装饰器的区别实例方法:不需要添加装饰器类方法:需要添加装饰器--》@classmethod静态方法:需要添加装饰器--》@staticmethod3.调用实例方法可以通过对象直接调用,需要用类名称创建一个对象类方法:可以直接通过类名称直接调用,也可以通过对象来调用静态方法:可以直接通过类名称直接调用,也可以通过对象来调用补充:静态方法不可以继承类方法不能访问实例变量,只能类变量class Dog():#定义类    age=3#类变量(不能传值)    def __init__(self):      self.name="xiaobai"#实例变量    def run(self):#实例方法      print("{}岁的{}正在跑步!".format(self.age,self.name))    @classmethod    def eat(cls):#类方法      #print(cls.name)类方法不能访问实例变量      print("xiaohei{}岁了".format(cls.age))#类方法只能方法类变量    @staticmethod    def sleep(name):#静态方法      #静态方法与类无关,只是类中的一个功能,静态方法不能访问实例变量和类变量      print("{}在睡觉".format(name))d=Dog()#创建对象d.run()#通过对象调用实例方法Dog.run(d)#通过类名称调用实例方法,需要在方法中传入实例对象Dog.eat()#通过类名称调用类方法d.eat()#通过对象调用类方法Dog.sleep("xiaohuang")#通过类名称调用静态方法d.sleep("xiaohuang")#通过对象调用静态方法单继承1.父类和子类的方法不一样class Father():#定义类    def __init__(self,car):      self.car=car    def rich(self):      print("有很多钱,买了一辆{}".format(self.car))    def house(self):      print("有很多楼")class Bigson(Father):#bigson类继承father类,bigson类拥有了father类的所有属性和方法    def gir(self):      print("给女朋友买了一辆{}".format(self.car))f=Father("宝马")f.rich()b=Bigson("奔驰")b.gir()2.子类拥有和父类相同的方法当子类拥有和父类相同方法时,通过子类实例调用该方法,执行的是子类下的方法class Teacher():#定义父类    def Name(self):#实例方法      print("This is my mother!")class Myself(Teacher):#定义子类继承父类    def Name(self):#实例方法      print("my name is xiaoyang!")M=Myself()M.Name()3.子类拥有和父类相同的方法(方法重写)class Teacher():#定义父类    def __init__(self,name):      self.name=name#实例变量    def Name(self):#实例方法      print("my teacher name is {}!".format(self.name))class Myself(Teacher):#定义子类继承父类    #对父类方法重写    def Name(self):#实例方法      print("my name is {}!".format(self.name))M=Myself("laowang")M.Name()多继承就是一个子类继承多个父类class Mother():    def bobby(self):      print("MOther love shopping!")class Farher():    def work(self):      print("Farher work is test engineer!" )class MYself(Farher,Mother):#继承多个父类    passM=MYself()#生成对象M.work()M.bobby()class Mother():    def __init__(self,something):      self.something=something    def bobby(self):      print("MOther love {}!".format(self.something))class Farher():    def __init__(self,work):      self.work=work    def Work(self):      print("Farher work is {}!" .format(self.work))class MYself(Farher,Mother):#继承多个父类    def __init__(self,work=None,something=None):      Farher.__init__(self,work)      Mother.__init__(self,something)    #想要调用特定父类的构造器只能使用"父类名称.__init__(self)"方式M=MYself('test',"shopping")#生成对象M.Work()M.bobby()#可以使用mro来查看顺序print(MYself.mro())如果不同的两个父类出现相同的属性或方法,子类会继承谁的属性或方法?父类中查询对应的方法,查询到第一个满足的方法之后就直接返回class Mother():    def __init__(self,work):      self.work=work    def bobby(self):      print("MOther love {}!".format(self.work))class Farher():    def __init__(self,work):      self.work=work    def bobby(self):      print("Farher work is {}!" .format(self.work))class MYself(Farher,Mother):#继承多个父类      passM=MYself('test')#生成对象M.bobby()#可以使用mro来查看顺序print(MYself.mro())
页: [1]
查看完整版本: python调用和继承