클래스가 다른 클래스로부터 어떻게 상속받을 수 있습니까? 나는 다음의 예에 의하여이를 구현하기 위해 노력하고 있습니다 :클래스가 다른 클래스로부터 어떻게 상속받을 수 있습니까?
class parents(object):
def __init__(self,dim_x, dim_y, nameprefix, sequence_number):
if not isinstance(dim_x, (int, float, long)):
raise TypeError("Dimension in x must be a number")
else:
self.sizex=str(int(dim_x))
if not isinstance(dim_y, (int, float, long)):
raise TypeError("Dimension in y must be a number")
else:
self.sizey=str(int(dim_y))
if not isinstance(nameprefix, string_types):
raise TypeError("The name prefix must be a string")
else:
self.prefix=nameprefix
if not isinstance(sequence_number, (int, float, long)):
raise TypeError("The sequence number must be given as a number")
else:
self.sqNr=str(int(sequence_number))
내가 클래스 child
가 prefix
과 parents
클래스
class child(parents):
def __init__(self,image_path='/vol/'):
self.IMG_PATH=image_path
self.ref_image=self.prefix+'_R_'+self.sqNr
logger.debug('Reference image: %s', self.ref_image)
에서 sqNr
상속하려면 다음과 같은 라인을 실행하지만,
>>> p=parents(300, 300, 'Test',0)
>>> p.prefix
'Test'
>>> c=child(p)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __init__
AttributeError: 'child' object has no attribute 'prefix'
내 구현, 어떤 잘못이 무엇인지 이해가 안 : 나는 오류 메시지가 제안?
그리고'image_path'는 어떻게됩니까? 초기 값을 변경하고 싶습니까? – Dalek
@Dalek 다른 기본 인수와 동일하게 'c = child (...,'alternative_path ')'를 전달하십시오. – jonrsharpe
+1 대 구성 - 중요한 구분. –