如何正确实例化这段代码?
How do I instantiate this piece of code properly?
我想通过将 M、F 声明为变量来实例化两个 child 类。从那里,我想打印结果实例的性别。
我正在密切关注示例,但我无法辨别为什么我的代码不起作用,而且我不断收到错误消息:“get_gender() 采用 0 个位置参数,但给出了 1 个”
如有任何建议,我将不胜感激
class Person:
class Male(Person):
def get_gender(self):
print("male")
class Female(Person):
def get_gender(self):
print("female")
M = Male() # instance
F = Female() # instance
M.get_gender()
F.get_gender()
Male
和 Female
不应嵌套在父 class Person
.
中
class Person:
pass
class Male(Person):
def get_gender(self):
print("male")
class Female(Person):
def get_gender(self):
print("female")
M = Male() # instance
F = Female() # instance
M.get_gender()
F.get_gender()
我想通过将 M、F 声明为变量来实例化两个 child 类。从那里,我想打印结果实例的性别。
我正在密切关注示例,但我无法辨别为什么我的代码不起作用,而且我不断收到错误消息:“get_gender() 采用 0 个位置参数,但给出了 1 个”
如有任何建议,我将不胜感激
class Person:
class Male(Person):
def get_gender(self):
print("male")
class Female(Person):
def get_gender(self):
print("female")
M = Male() # instance
F = Female() # instance
M.get_gender()
F.get_gender()
Male
和 Female
不应嵌套在父 class Person
.
class Person:
pass
class Male(Person):
def get_gender(self):
print("male")
class Female(Person):
def get_gender(self):
print("female")
M = Male() # instance
F = Female() # instance
M.get_gender()
F.get_gender()