我上了以下三节课India
是States
的父类,States
是District
的父类。我定义了一个District
类的对象。当我尝试运行India
类的方法时,它给出了一个错误。请帮助我如何运行它
class India:
def __init__(self):
print("Country is India")
def functionofindia(number):
rvalue=number*2
return rvalue
class States(India):
def __init__(self,nameofstate):
super().__init__()
print("state is {}".format(nameofstate))
class District(States):
def __init__(self,nameofstate, nameofdistrict):
super().__init__(nameofstate)
print("District is {}".format(nameofdistrict))
HP=District("Himachal Pradesh","Mandi")
print(HP.functionofindia(2))
错误是:
Country is India
state is Himachal Pradesh
District is Mandi
Traceback (most recent call last):
File "c:\Users\DELL\OneDrive\Desktop\practice\oops.py", line 23, in <module>
print(HP.functionofindia(2))
TypeError: functionofindia() takes 1 positional argument but 2 were given
要使用这样的类方法,应该将
self
作为方法定义中的第一个参数传递。所以你们班的印度变成了:当方法与类的任何属性都不相关时,也可以用静态方法替换它。请参阅有关静态方法here的详细信息。采用静态方法的印度:
相关问题 更多 >
编程相关推荐