了解子类和 __init__ 方法
Understanding subclasses and __init__ method
我有一个 question.I 正在学习 Python 并试图理解 superclasses、subclasses 和 them.I 之间的层次结构我不完全理解的一件事。
我正在尝试编写一个程序,其中有一个 "Person" superclass 和 "Birthday" subclass.I 定义的 init 方法 them.What 我想做的是,我想创建一个 Person.At 的实例,同时它应该为 Birthday.Because 创建相同的实例,所有人都有生日 :) 我想使用这些方法我在 "Birthday" class 中定义的。
代码;
import datetime
class Person(object):
def __init__(self,name):
self.name=name
Birthday.__init__(self,name)
def gender(self,gender):
self.gender=gender
class Birthday(Person):
def __init__(self,name):
self.objectlike=[]
self.birthday=None
self.youget={}
def setbirth(self,year,month,day):
self.birthday=datetime.date(year,month,day)
def getbirth(self):
return self.birthday
def __str__(self):
return self.name
def setyouget(self,year,thing):
self.youget[year]=thing
def setlikeob(self,thing):
self.objectlike.append(thing)
def nextbirth(self):
thisyearbirth=getbirth().year.replace(year=datetime.date.today().year)
return thisyearbirth
def howlong(self):
return self.nextbirth()-datetime.date.today()
def getage(self):
age=datetime.date.today().year-self.birthday.year
return age
问题是当我创建一个 Person 的实例时,例如,
joey=Person('Joey Tribiani')
那我试着给joey定个生日;
joey.setbirth(1980,5,5)
它说:'Person'对象没有属性'setbirth'
我该如何克服这个问题?我想我应该在 init 方法中添加一些代码,但我不知道要 add.I 添加什么 "Birthday.init(self,name)" 到 Person class 但它没有没用。
(顺便说一句,这是我第一次 question.I 阅读许多关于此的主题 website.I 我很惊讶人们使用它的帮助。)
根据上面粘贴的代码 Person is the Superclass
和 Birthday is the Subclass
。所以,是子类继承自超类,而不是相反。
此外,Inheritance
是一种 is-a
关系。 Have/Has-A
关系对应composition
。您可以阅读更多 here.
继承实际上有两个方面 - 子类型化(语义)和实现继承(技术)。
从语义上讲,继承描述了一种 "is a" 关系 - 如果 B 是 A 的子class,那么 B "is a" A(有一些特殊化),你应该能够以相同的方式使用 A 和 B 的实例(这称为 Liskov 替换原则)。
从技术上讲,继承是一种受限的组合/委托(从语义上讲,是一种 "has a" 关系)- subclass 具有对其父 class 的引用,并且未在子 class 中定义的方法(和其他 class 属性)在父级(及其父级等)中查找。这意味着继承也可以用于代码重用 ("implementation inheritance"),最终不遵守 liskov 替换原则 ("type inheritance")
请注意,在像 Python 这样的动态类型语言中,您实际上 不需要 子类型继承。你可以有一个 class B 实现与 class A 完全相同的接口 - 根据 Liskov 的说法,这是一个适当的子类型 - 根本不继承 A,也不与 A 有任何共同的祖先。
现在回到你的代码...第一个明显的错误是使 Birthday
成为 Person
的子class,因为生日显然不是一个人(语义上,关系是一个组合:一个人 有 生日)。第二个明显的错误是认知错误:看起来你错误地理解了继承关系。如果 Birthday
继承自 Person
,那么 Birthday
class 可以访问 Person
方法,而不是相反。
还有其他明显的问题,但它们在 ATM 上并不重要。首先根据 "has a"(组合/委托)重新考虑您的代码,然后是时候解决这些问题了。
我有一个 question.I 正在学习 Python 并试图理解 superclasses、subclasses 和 them.I 之间的层次结构我不完全理解的一件事。 我正在尝试编写一个程序,其中有一个 "Person" superclass 和 "Birthday" subclass.I 定义的 init 方法 them.What 我想做的是,我想创建一个 Person.At 的实例,同时它应该为 Birthday.Because 创建相同的实例,所有人都有生日 :) 我想使用这些方法我在 "Birthday" class 中定义的。 代码;
import datetime
class Person(object):
def __init__(self,name):
self.name=name
Birthday.__init__(self,name)
def gender(self,gender):
self.gender=gender
class Birthday(Person):
def __init__(self,name):
self.objectlike=[]
self.birthday=None
self.youget={}
def setbirth(self,year,month,day):
self.birthday=datetime.date(year,month,day)
def getbirth(self):
return self.birthday
def __str__(self):
return self.name
def setyouget(self,year,thing):
self.youget[year]=thing
def setlikeob(self,thing):
self.objectlike.append(thing)
def nextbirth(self):
thisyearbirth=getbirth().year.replace(year=datetime.date.today().year)
return thisyearbirth
def howlong(self):
return self.nextbirth()-datetime.date.today()
def getage(self):
age=datetime.date.today().year-self.birthday.year
return age
问题是当我创建一个 Person 的实例时,例如,
joey=Person('Joey Tribiani')
那我试着给joey定个生日;
joey.setbirth(1980,5,5)
它说:'Person'对象没有属性'setbirth'
我该如何克服这个问题?我想我应该在 init 方法中添加一些代码,但我不知道要 add.I 添加什么 "Birthday.init(self,name)" 到 Person class 但它没有没用。
(顺便说一句,这是我第一次 question.I 阅读许多关于此的主题 website.I 我很惊讶人们使用它的帮助。)
根据上面粘贴的代码 Person is the Superclass
和 Birthday is the Subclass
。所以,是子类继承自超类,而不是相反。
此外,Inheritance
是一种 is-a
关系。 Have/Has-A
关系对应composition
。您可以阅读更多 here.
继承实际上有两个方面 - 子类型化(语义)和实现继承(技术)。
从语义上讲,继承描述了一种 "is a" 关系 - 如果 B 是 A 的子class,那么 B "is a" A(有一些特殊化),你应该能够以相同的方式使用 A 和 B 的实例(这称为 Liskov 替换原则)。
从技术上讲,继承是一种受限的组合/委托(从语义上讲,是一种 "has a" 关系)- subclass 具有对其父 class 的引用,并且未在子 class 中定义的方法(和其他 class 属性)在父级(及其父级等)中查找。这意味着继承也可以用于代码重用 ("implementation inheritance"),最终不遵守 liskov 替换原则 ("type inheritance")
请注意,在像 Python 这样的动态类型语言中,您实际上 不需要 子类型继承。你可以有一个 class B 实现与 class A 完全相同的接口 - 根据 Liskov 的说法,这是一个适当的子类型 - 根本不继承 A,也不与 A 有任何共同的祖先。
现在回到你的代码...第一个明显的错误是使 Birthday
成为 Person
的子class,因为生日显然不是一个人(语义上,关系是一个组合:一个人 有 生日)。第二个明显的错误是认知错误:看起来你错误地理解了继承关系。如果 Birthday
继承自 Person
,那么 Birthday
class 可以访问 Person
方法,而不是相反。
还有其他明显的问题,但它们在 ATM 上并不重要。首先根据 "has a"(组合/委托)重新考虑您的代码,然后是时候解决这些问题了。