类的继承简单用法
class iphone: #父类 def __init__(self,name,game,photograph): self.name=name self.game=game self.photograph=photograph def function(self): print("{}手机具备有{}功能".format(self.name,self.photograph)) def business(self): print("{}手机还可以{}功能".format(self.name,self.game)) class iphonese(iphone): #子类 pass data=iphonese("苹果手机","游戏","拍照") data.function() data.business() --------------------打印结果-------------------- 苹果手机手机具备有拍照功能 苹果手机手机还可以游戏功能
(直接继承父类的所有方法和属性,不做任何改动。最简单的类的继承法,子类不需要做任何事情,就可以调用父类的所有属性和方法)
类继承中的重写与拓展重写
重写是对继承过来的父类的方法进行重写
class iphone: #父类 def __init__(self,name,game,photograph): self.name=name self.game=game self.photograph=photograph def function(self): print("{}手机具备有{}功能".format(self.name,self.photograph)) def business(self): print("{}手机还可以{}功能".format(self.name,self.game)) class iphonese(iphone): #子类 def function(self): print("{}手机的{}功能非常的好用,非常的强大,像素非常高".format(self.name,self.photograph)) data=iphonese("苹果手机","游戏","拍照") data.function() ---------------------打印结果--------------------- 苹果手机手机的拍照功能非常的好用,非常的强大,像素非常高
拓展
class iphone: #父类 def __init__(self,name,game,photograph): self.name=name self.game=game self.photograph=photograph def function(self): print("{}手机具备有{}功能".format(self.name,self.photograph)) def business(self): print("{}手机还可以{}功能".format(self.name,self.game)) class iphonese(iphone): #子类 def shopping(self,shopping,internet): print("新一代的{}手机还可以支持{}功能和{}功能,功能真多".format(self.name,shopping,internet)) data=iphonese("苹果手机","游戏","拍照") data.shopping("摄像","相册") ------------------------打印结果------------------------ 新一代的苹果手机手机还可以支持摄像功能和相册功能,功能真多
类的超继承
名词解释:类继承还有另外一种特殊用法,就是超继承。超继承其实也是类方法重写的另一种表现形式,超继承既要有父类的特性又要有子类的新特性,是个非常贪婪的继承方法
class iphone: #父类 def __init__(self,name,game,photograph): self.name=name self.game=game self.photograph=photograph def function(self,data): print("{}手机具备有{}功能".format(self.name,data)) def business(self): print("{}手机还可以{}功能".format(self.name,self.game)) class iphonese(iphone): #子类 def shopping(self,shopping,internet): print("新一代的{}手机还可以支持{}功能和{}功能,功能真多".format(self.name,shopping,internet)) def function(self,data): # super 超类 super().function(data) print("{}现在还具备有看电影".format(self.name)) data=iphonese("苹果手机","游戏","拍照") data.function("上微信") ---------------------------打印结果--------------------------- 苹果手机手机具备有上微信功能 苹果手机现在还具备有看电影
1) 实例属性和类属性的优先级?同名会不会覆盖?------- 对于对象而言,自己有就用自己的,自己没有,就用类的。自己和类都没有,就报错。 2) 类属性修改? -- 类方法(所有实例的共性) cls - 代表当前类 @classmethod def 类方法名(cls): 方法实现 与实例方法的区别: 实例方法:self 对象调用 类方法:cls @classmethod 类和对象都可以调用。一般是类去调用 静态方法:没有一定要传self或者cls @staticmethod 类和对象都可以调用 3) 静态方法 -- 普通函数 - 没有必定要传的参数-类或者对象。只不过呢,它是放在类里面。 @staticmethod def 函数名称(): 函数实现
类属性修改:
修改类属性:@classmethod class Adde: country="中国香港" def qi(self): print("我们是{}人".format(self.country)) @classmethod def qita(cls): cls.country="中国台湾" print("我们是{}人".format(cls.country)) print("类的属性是",Adde.country) addtest=Adde() addtest.qi() addtest.qita() -----------------------打印结果----------------------- 类的属性是 中国香港 我们是中国香港人 我们是中国台湾人
静态方法:
class Adde: country="中国香港" def qi(self): print("我们是{}人".format(self.country)) @staticmethod #加入了之后,那么我们在qita()就可以不加self def qita(): print("我们是中国人") addtest=Adde() addtest.qi() addtest.qita() -----------------------打印结果----------------------- 我们是中国香港人 我们是中国人
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)