使用python在面向对象编程中创建方法 [英] creating a method in object oriented programming with python
问题描述
我正在学习python中的面向对象编程,但我不太确定如何为类编写方法
I'm learning object oriented programming in python and I'm not too sure how to write methods for classes
-
我的第一个问题是,您可以同时继承两个类吗?
示例:
My first question is, can you inherit two classes at the same time? Example:
class A:
def __init__ eg. storing list of strings
class B:
def __ init__ eg. storing list of strings
# I want to inherit both class A and B into class C
class C (A,B):
这可能吗?许多示例仅显示从一个类继承。
Is this possible? many examples showed only inheritance from one class.
假定我可以从两个类A
和 B类
,我需要组合 A类 c的实例和
类的实例B
。例如, Class A
存储名称列表, class B
存储头发颜色列表。在 C类
中,我想将名称和头发颜色加在一起,以便告诉谁头发的颜色。我不太确定如何将这两个对象加在一起,但这是我的尝试。您可以通过提出解决方法的建议来帮助我吗?
Assuming that I can import the methods from both class A
and class B
, I need to combine the instances in class A
and instances in class B
. For example, class A
stores list of names, class B
stores a list of hair colours . In class C
I want to add names and hair colours together so that I can tell who's having what hair colour. I'm not too sure how to add those two objects together but here's my attempt. Can you help me by giving suggestions of how to tackle this?
class A:
def __init__(self,name):
self.name= name
def getName(self):
return self.name # this is so that i whenever i call the object in my class C, it will return the name
class B:
def __init__(self,hair):
self.hair = hair
def getHair (self):
return self.hair
class C(A,B):
def __init__(self):
A.__init__(self,name)
B.__init__(self,hair)
self.add= [self,A,B]
def add(self,name,hair): # my method to combine instances in class A and B
self.add.append[name,hair]
之后解决了这个添加问题,即存储谁的头发颜色的信息,我的目标是在 Class C
中创建另一个方法,该方法将列出所有具有相同头发的名称我提供头发颜色参数时的颜色。我不能不首先添加两个实例来做到这一点。
After solving this add issue, i.e. storing information of who's having what hair colour, my goal is to create another method in class C
which will list me all the names that have the same hair colour when I provide the hair colour argument. I cannot do this without adding the two instances first.
我试图通过给每个类几个对象来运行它。 Class A
和 Class B
没关系,我可以让程序返回名字和头发的颜色。但是问题出在我尝试执行 .add
部分之后。它给我一个消息错误,指出
I've tried to run this by giving each of the class several objects. It was ok for class A
and class B
, I can get the program to return me the name and the hair colour. But the problem came after I was trying to execute the .add
part. It gives me a message error that says
TypeError: 'list' object is not callable
请告诉我整个事情是否错误,我应该开始使用其他方法编写,否则我的程序仍然可以挽救。
Please tell me whether the whole thing is wrong and I should start writing using different approach or my program can still be rescued.
推荐答案
第一个问题。答案是肯定的。
First question. Answer is yes.
在类定义之后,您需要一些东西。有时,所有行为都由A和B定义,因此您只需要在其中放置 pass
(或文档字符串)
You need something after the class definition. Sometimes all the behaviour is defined by A and B, so you just need to put pass
there (or a docstring)
class C(A, B):
pass
第二个问题:append是一个函数,您需要用括号调用
Second question: append is a function, you need to call it with parentheses
self.add.append((name, hair))
您不能有一个属性 add
和方法 add
,因为它们共享相同的名称空间。您最后定义的一个将替换另一个
You can't have an attribute add
and a method add
as they share the same namespace. Whichever one you define last will replace the other
这篇关于使用python在面向对象编程中创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!