
正文
python继承函数重载 python中继承的定义
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
怎么理解Python中重载?通俗理解是
python里面没有重载吧?
正常python继承函数重载的重载说的应该是函数方法的重载,即两个函数的名称相同但是参数不同是重载python继承函数重载,会出现重载的情况一般是函数实现相同功能时python继承函数重载,遇到参数类型可能不同的情况python继承函数重载,会出现重载,如比较常见的对于加法的实现,如果输入两个整数相加则直接使用整数加法,如果两个字符串相加则使用字符串的拼接。输入的参数类型不同,其实是两个不同的函数,这在那种强类型语言中是可以利用函数重载来实现的,而python中函数类型是动态的各种各样的,所以不需要重载这个机制。相同函数名就是表示同一个方法。
相关问答
Q1: 如何解决python2不能隐式继承
继承是所有开发语言的必修内容,而本文写的只是Python继承中的特殊之处,关于继承概念及内容可以自行百度(不装B,感觉百度挺好的)1.构造函数:
要说继承,先要说一下构造函数。Java要求是与类名相同并且无返回值,而Python则是强制要求命名为“__init__()”。
当创建类的对象时,会自动先调用构造函数,一般用于初始化。构造函数可以不写,那么程序会隐式自动增加一个空的构造函数。
2.继承写法:
(1).class 空格 类名称 括号内填写父类名 冒号具体写法如下class A:
def __init__(self):
pass
def print_class_name(self):
print "this is class A"
class B(A):
def __init__(self):
pass
if __name__ == "__main__":
class_b = B()
class_b.print_class_name()
上面代码首先定义了一个名为“A”的类,包含一个名为“print_class_name”的方法。然后,定义一个名为“B”的类,继承“A”,同时继承了“A”类的“print_class_name”的方法。
此时“A”类为“B”类的父类或者叫基类,“B”类是“A”类的子类,子类会继承父类的所有公共方法。
(2).意义:
一字记之曰“懒!”(感叹号不算字)python继承函数重载我始终相信赖人才能推动科学进步。
言归正传,假如python继承函数重载你要写一个老王类,包含年龄、性别等方法,后面还要写一个老王的儿子小王类,也有年龄、性别等方法?
class FatherWang:
def __init__(self, age=43, sex='man'):
self.a = age
self.s = sex
def age(self):
print self.a
def sex(self):
print self.s
class SonWang:
def __init__(self, age=13, sex='man'):
self.a = age
self.s = sex
def age(self):
print self.a
def sex(self):
print self.s
if __name__ == "__main__":
father = FatherWang(43, "man")
father.age()
father.sex()
son = SonWang(13, "man")
son.age()
son.sex()
你会发现两个类中有相同名称和功能的方法,这样写岂不是很重复很累?(尽管按键盘次数不算太多,我依然觉得很累)如果有继承就很好解决了。
class FatherWang:
def __init__(self, age=43, sex='man'):
self.a = age
self.s = sex
def age(self):
print self.a
def sex(self):
print self.s
class SonWang(FatherWang):
def __init__(self, age=13, sex='man'):
FatherWang.__init(age, sex)
if __name__ == "__main__":
father = FatherWang(43, "man")
father.age()
father.sex()
son = SonWang(13, "man")
son.age()
son.sex()
两者运行结果完全一样,但是使用继承方法却省了很多按键盘的次数。
3.经典类与新式类:
(1)经典类写法:
class A:
pass
(2)新式类写法:
class A(object):
pass
可以看出,新式类和经典类的区别在于,是否继承object这个基类。object是所有类的父类。所以之前不带“(object)”的写法,属于经典类写法,加上“(object)”就是新式类的写法。
(3).原因:这里我得吐槽一下Python的版本混乱。2.2版本之前只有经典类写法,这里有一个问题,代码如下?
class A:
pass
class B(object):
pass
a = A()
b = B()
print a.__class__
print type(a)
print "----------"
print b.__class__
print type(b)
结果为:
__main__.A
type 'instance'
----------
class '__main__.B'
class '__main__.B'
首先A类为经典类,B类为新式类。__class__属性和type()方法都是返回对象类型,那么问题来了,使用经典类的写法返回结果却不一致。因此在2.2版本之后出现了新式类来解决这个问题,自然,新式类和经典类还有更大的区别在后面说。另外在3.3版本中,无论使用哪种写法,python都会隐式的继承object,所以3.3版本不会再有经典类(在这里我只想问,早干什么去了!),但是鉴于3.3兼容性问题,貌似没有太多人用。
4.方法重写与方法重载
(1).方法重写:
class FatherWang:
def __init__(self, age=43, sex='man'):
self.a = age
self.s = sex
def age(self):
print self.a
def sex(self):
print self.s
def name(self):
print "Wang_yang"
class SonWang(FatherWang):
def __init__(self, age=13, sex='man'):
FatherWang.__init(age, sex)
def name(self):
print "Wang_xiaoming"
if __name__ == "__main__":
father = FatherWang(43, "man")
father.age()
father.sex()
father.name()
son = SonWang(13, "man")
son.age()
son.sex()
son.name()
比继承写法(2)中的代码相比,两个类分别多了同名的方法“name”,之前说过子类会继承父类的方法,那么这时候两个类有相同名字的方法,冲突了,怎么处理?
这个时候,就叫方法重写。可以理解为,子类的“name”方法把父类的“name”方法覆盖了,重新写了,所以调用子类的“name”方法时,会以子类的为准(尽管这种理解并不准确,但是可以很好解释“方法重写”这个名词,后面会讲到正确理解)。
注意下面的代码
class FatherWang:
def __init__(self, age=43, sex="man"):
self.a = age
self.s = sex
print "I am FatherWang"
def age(self):
print "Father age:"+str(self.a)
def sex(self):
print "Father sex:"+str(self.s)
class MotherLi:
def __init__(self, age=40, sex="woman"):
self.a = age
self.s = sex
print "I am MotherLi"
def age(self):
print "Mother age:"+str(self.a)
def sex(self):
print "Mother sex"+str(self.s)
class SonWang(FatherWang, MotherLi):
def __init__(self, age=13, sex="man"):
FatherWang.__init__(self, age, sex)
MotherLi.__init__(self, age, sex)
print "I am SonWang"
if __name__ == "__main__":
son = SonWang()
son.age()
son.sex()
执行结果:
I am FatherWang
I am MotherLi
I am SonWang
Father age:13
Father sex:man
在之前代码上稍作修改,另外增加了一个MotherLi的类,SonWang类继承了FatherWang类和MotherLi类。注意,这是经典类的写法。
首先,我们知道了python多继承的写法,就是在括号中上一个父类后面加个逗号,然后再写上下一个父类的名字:
class SonWang(FatherWang, MotherLi):
其次,FatherWang类和MotherLi类,都有名为age和sex方法,SonWang类为什么会继承FatherWang类的方法呢?那么把SonWang类的继承顺序改一下class SonWang(MotherLi, FatherWang):
就会发现继承的是MotherLi类的方法。
通过结果可知,是按照继承的顺序。
让我们把代码结构变得更发杂一些吧,我想会崩溃的,哈哈哈?
class Grandfather:
def __init__(self, age=73, sex="man"):
self.a = age
self.s = sex
print "I am Grandfather"
def age(self):
print "Grandfather age:"+str(self.a)
def sex(self):
print "Grandfather sex:"+str(self.s)
def Interesting(self):
print "Grandfather Interesting"
class Grandmother:
def __init__(self, age=70, sex="woman"):
self.a = age
self.s = sex
print "I am Grandmother"
def age(self):
print "Grandmother age:"+str(self.a)
def sex(self):
print "Grandmother sex:"+str(self.s)
def Interesting(self):
print "Grandmother Interesting"
class FatherWang(Grandfather, Grandmother):
def __init__(self, age=43, sex="man"):
self.a = age
self.s = sex
Grandfather.__init__(self, age, sex)
Grandmother.__init__(self, age, sex)
print "I am FatherWang"
def age(self):
print "Father age:"+str(self.a)
def sex(self):
print "Father sex:"+str(self.s)
class MotherLi(Grandfather, Grandmother):
def __init__(self, age=40, sex="woman"):
self.a = age
self.s = sex
Grandfather.__init__(self, age, sex)
Grandmother.__init__(self, age, sex)
print "I am MotherLi"
def age(self):
print "Mother age:"+str(self.a)
def sex(self):
print "Mother sex"+str(self.s)
def Interesting(self):
print "MotherLi Interesting"
class SonWang(FatherWang, MotherLi):
def __init__(self, age=13, sex="man"):
FatherWang.__init__(self, age, sex)
MotherLi.__init__(self, age, sex)
print "I am SonWang"
if __name__ == "__main__":
son = SonWang()
son.age()
son.sex()
son.Interesting()
执行结果:
I am Grandfather
I am Grandmother
I am FatherWang
I am Grandfather
I am Grandmother
I am MotherLi
I am SonWang
Father age:13
Father sex:man
Grandfather Interesting
话说,我自己都有点儿晕。简单来讲,就是儿子继承了老爸、老妈,然后老爸继承了爷爷、奶奶,妈妈继承了老爷、姥姥。(真是一大家子啊)通过执行结果可知,儿子类先找到老爸类,然后再找老爸类的第1个父类爷爷类,此时发现爷爷类没有父类了,那么执行初始化。然后还要继续找到老爸类的第2个父类奶奶类,此时发现奶奶类没有父类了,执行初始化。此时老爸类的所有父类都初始化完成,初始化自己。然后开始找妈妈类……那么为什么Interesting方法会使用爷爷类的呢?奶奶类、老爷类、姥姥类都有啊?首先儿子类没有Interesting方法,会先找第1个父类老爸类。发现老爸类也没有,再找老爸类的第1个父类,发现找到了,那么就直接调用不再往下找了。
结论:经典类的多继承,按照继承顺序查找。即,从左到右,从下到上的方式。注意,只有经典类是这样的!
(2).新式类的多继承:
class Grandfather(object):
def __init__(self, age=73, sex="man"):
self.a = age
self.s = sex
print "I am Grandfather"
def age(self):
print "Grandfather age:"+str(self.a)
def sex(self):
print "Grandfather sex:"+str(self.s)
def Interesting(self):
print "Grandfather Interesting"
class Grandmother(object):
def __init__(self, age=70, sex="woman"):
self.a = age
self.s = sex
print "I am Grandmother"
def age(self):
print "Grandmother age:"+str(self.a)
def sex(self):
print "Grandmother sex:"+str(self.s)
def Interesting(self):
print "Grandmother Interesting"
class FatherWang(Grandfather, Grandmother):
def __init__(self, age=43, sex="man"):
self.a = age
self.s = sex
Grandfather.__init__(self, age, sex)
Grandmother.__init__(self, age, sex)
print "I am FatherWang"
def age(self):
print "Father age:"+str(self.a)
def sex(self):
print "Father sex:"+str(self.s)
class MotherLi(Grandfather, Grandmother):
def __init__(self, age=40, sex="woman"):
self.a = age
self.s = sex
Grandfather.__init__(self, age, sex)
Grandmother.__init__(self, age, sex)
print "I am MotherLi"
def age(self):
print "Mother age:"+str(self.a)
def sex(self):
print "Mother sex"+str(self.s)
def Interesting(self):
print "MotherLi Interesting"
class SonWang(FatherWang, MotherLi):
def __init__(self, age=13, sex="man"):
FatherWang.__init__(self, age, sex)
MotherLi.__init__(self, age, sex)
print "I am SonWang"
if __name__ == "__main__":
son = SonWang()
son.age()
son.sex()
son.Interesting()
执行结果:
I am Grandfather
I am Grandmother
I am FatherWang
I am Grandfather
I am Grandmother
I am MotherLi
I am SonWang
Father age:13
Father sex:man
MotherLi Interesting
Q2: python 重载方法有哪些特点
python 的重载主要包括方法重载和运算符重载。1.python 方法重载: 其他的语言一般对于方法重载的话,主要是根据参数的类型不同或者是数量不同来区分同名的方法。而python则比较特殊,它本身是动态语言,方法的参数是没有类型的,当调用传值的时候才确定参数的类型,故对参数类型不同的方法无需考虑重载。对参数数量不同的方法,则(大多数情况下)可以采用参数默认值来实现。比如你可以定义函数的默认值:def info(x,y,z=1): pass2.python 运算符重载: 在C#中,我们通过使用关键字operator定义一个运算符方法,并定义与所在类相关的运算符行为。在 Python中,运算符重载的方式更为简单——每一个类都默认内置了所有可能的运算符方法,只要重写这个方法,就可以实现针对该运算符的重载。例如以下是重载加法操作:class Info(object): def __init__(self): self.a = 11 self.b = 22 def __add__(self,x): return self.a * self.b 上面的例子是重写了+操作符号,你也可以重载其他的运算符。比如你可以重载乘号运算符,感兴趣的话,可以自己写写代码。希望上面讲的2点能够让你对python重载有个简单的认识。有兴趣的可以关注下。
Q3: 为什么 Python 不支持函数重载
重载和重写,这是两个新概念,是两个令我们容易混淆的概念。方法重载(overloadingmethod)是在一个类里面,方法名字相同,而参数不同。返回类型呢?可以相同也可以不同。方法重写(overidingmethod)子类不想原封不动地继承父类的方法,而是想作一定的修改,这就需要采用方法的重写。方法重写又称方法覆盖。方法重载是让类以统一的方式处理不同类型数据的一种手段。Java的方法重载,就是在类中可以创建多个方法,它们具有相同的名字,但具有不同的参数和不同的定义。调用方法时通过传递给它们的不同个数和类型的参数来决定具体使用哪个方法,这就是多态性。方法重写:在Java中,子类可继承父类中的方法,而不需要重新编写相同的方法。但有时子类并不想原封不动地继承父类的方法,而是想作一定的修改,这就需要采用方法的重写。方法重写又称方法覆盖。若子类中的方法与父类中的某一方法具有相同的方法名、返回类型和参数表,则新方法将覆盖原有的方法。如需父类中原有的方法,可使用super关键字,该关键字引用了当前类的父类重写方法的规则:参数列表必须完全与被重写的方法的相同,否则不能称其为重写而是重载.返回的类型必须一直与被重写的方法的返回类型相同,否则不能称其为重写而是重载.访问修饰符的限制一定要大于被重写方法的访问修饰符(publicprotecteddefaultprivate)重写方法一定不能抛出新的检查异常或者比被重写方法申明更加宽泛的检查型异常.例如,父类的一个方法申明了一个检查异常IOException,在重写这个方法是就不能抛出Exception,只能抛出IOException的子类异常,可以抛出非检查异常.重载的规则:必须具有不同的参数列表;可以有不同的返回类型,只要参数列表不同就可以了;可以有不同的访问修饰符;可以抛出不同的异常;注意,Java的方法重载要求同名的方法必须有不同的参数表,仅有返回类型不同是不足以区分两个重载的方法。重写方法只能存在于具有继承关系中,重写方法只能重写父类非私有的方法。下面分别举一个例子来说明方法重载:publicclassTestOverLoad{publicstaticvoidmain(String[]args){Testtest=newTest();test.print(null);}}classTest{publicvoidprint(Stringsome){System.out.println("Stringversionprint");}publicvoidprint(Objectsome){System.out.println("Objectversionprint");}}该程序输出的结果是Stringversionprint。
Q4: 关于python的重载问题
没有重载python继承函数重载,但是可以有默认参数和不定长参数python继承函数重载,可以判断默认值和参数长度来处理。
比如:
def range(start, end = -1):
if end == -1:
end = start
start = 0
或
def range(*args):
if len(args) == 1:
start = 0
end = args[0]
elif len(args) == 2:
start, end = args[0], args[1]
python继承函数重载的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于python中继承的定义、python继承函数重载的信息别忘了在本站进行查找喔。







