在 python 中将实例类型更改为继承
Change instance type to inheritance in python
我有一个 class Person
和 2 classes Child
和 Adult
继承自 `Person.
class Person:
def __init__(self, name):
self.name = name
self.age = 0
def say_hi(self):
print("Hi, I am " + self.name)
def set_age(self, age):
self.age = age
class Child(Person):
def play_with_children(self):
print("Playing with children")
class Adult(Person):
def drink_alcohol(self):
print("drinking")
我想从 Person
创建一个实例 然后 将它设置为 age
根据它,这个实例应该 "become" a Child
或 Adult
.
此外,如果它是 Child
并且增加了 age
,我希望它增加到 "become" 和 Adult
。
请注意,此代码有一个错误,因为 Person
的构造函数需要一个 name
,因为它旨在创建一个新实例。
person1 = Person("Marvin")
person1.set_age(15)
if person1.age < 21:
person1 = Child()# This is an error => expects the name
#But I wouldn't like to handle all members like this => person1 = Child(person1.name)
#I don't want a new instance, is that even posible?
person1.say_hi()
person1.play_with_children()
person1.set_age(25)
if person1.age > 21:
person1 = Adult()# This is an error => expects the name
#But I wouldn't like to handle all members like this => person1 = Adult(person1.name)
#I don't want a new instance, is that even posible?
person1.say_hi()
person1.drink_alcohol()
这可能吗?
是否可以在不创建新实例的情况下完成?
PS:这只是一个示例代码,显示了我遇到的一个更复杂的问题(不是真正的儿童和成人:D)。
如评论中所述,在 Python 3 中我们可以访问任何实例的 __class__
并更改它。
虽然你必须小心,但你可以将几个类的属性添加到同一个实例并创建一个奇怪的怪物(方法是安全的)。
person1 = Person("Marvin")
person1.set_age(15)
if person1.age < 21:
person1.__class__ = Child
person1.say_hi()
person1.play_with_children()
person1.set_age(25)
if person1.age > 21:
person1.__class__ = Adult
person1.say_hi()
person1.drink_alcohol()
所以怪物:
class Dog:
def wouf(self):
print("wouf")
person1.__class__ = Dog
print("Attributes are saved, name: "+ person1.name)
#But methods not, so now I can't use person1.say_hi()
person1.wouf()
我有一个 class Person
和 2 classes Child
和 Adult
继承自 `Person.
class Person:
def __init__(self, name):
self.name = name
self.age = 0
def say_hi(self):
print("Hi, I am " + self.name)
def set_age(self, age):
self.age = age
class Child(Person):
def play_with_children(self):
print("Playing with children")
class Adult(Person):
def drink_alcohol(self):
print("drinking")
我想从 Person
创建一个实例 然后 将它设置为 age
根据它,这个实例应该 "become" a Child
或 Adult
.
此外,如果它是 Child
并且增加了 age
,我希望它增加到 "become" 和 Adult
。
请注意,此代码有一个错误,因为 Person
的构造函数需要一个 name
,因为它旨在创建一个新实例。
person1 = Person("Marvin")
person1.set_age(15)
if person1.age < 21:
person1 = Child()# This is an error => expects the name
#But I wouldn't like to handle all members like this => person1 = Child(person1.name)
#I don't want a new instance, is that even posible?
person1.say_hi()
person1.play_with_children()
person1.set_age(25)
if person1.age > 21:
person1 = Adult()# This is an error => expects the name
#But I wouldn't like to handle all members like this => person1 = Adult(person1.name)
#I don't want a new instance, is that even posible?
person1.say_hi()
person1.drink_alcohol()
这可能吗?
是否可以在不创建新实例的情况下完成?
PS:这只是一个示例代码,显示了我遇到的一个更复杂的问题(不是真正的儿童和成人:D)。
如评论中所述,在 Python 3 中我们可以访问任何实例的 __class__
并更改它。
虽然你必须小心,但你可以将几个类的属性添加到同一个实例并创建一个奇怪的怪物(方法是安全的)。
person1 = Person("Marvin")
person1.set_age(15)
if person1.age < 21:
person1.__class__ = Child
person1.say_hi()
person1.play_with_children()
person1.set_age(25)
if person1.age > 21:
person1.__class__ = Adult
person1.say_hi()
person1.drink_alcohol()
所以怪物:
class Dog:
def wouf(self):
print("wouf")
person1.__class__ = Dog
print("Attributes are saved, name: "+ person1.name)
#But methods not, so now I can't use person1.say_hi()
person1.wouf()