在python中将实例类型更改为继承 [英] Change instance type to inheritance in python
问题描述
我有一个类 Person
和两个类 Child
和 Adult
继承自 `Person.
我想从 Person
创建一个实例 然后 设置它的 age
并且根据它这个实例应该成为"一个 儿童
或成人
.
另外,如果它是一个 Child
并且它的 age
增加,我希望它成为"一个 Adult
.请注意,此代码有一个错误,因为 Person
的构造函数需要一个 name
,因为它旨在创建一个新实例.
person1 = Person("Marvin")person1.set_age(15)如果 person1.age <21:person1 = Child()# 这是一个错误 =>期待名字#但我不想像这样处理所有成员=>person1 = Child(person1.name)#我不想要一个新实例,这可能吗?person1.say_hi()person1.play_with_children()person1.set_age(25)如果 person1.age >21:person1 = Adult()# 这是一个错误 =>期待名字#但我不想像这样处理所有成员=>person1 = 成人(person1.name)#我不想要一个新实例,这可能吗?person1.say_hi()person1.drink_alcohol()
这可能吗?
这可以在不创建新实例的情况下完成吗?
PS:这只是一个示例代码,显示了我遇到的一个更复杂的问题(不是真正的儿童和成人:D).
正如评论中提到的,在 Python 3 中,我们可以访问任何实例的 __class__
并对其进行更改.
虽然你必须小心,但你可以将多个类的属性添加到同一个实例并创建一个奇怪的怪物(方法是安全的).
person1 = Person("Marvin")person1.set_age(15)如果 person1.age <21:person1.__class__ = 孩子person1.say_hi()person1.play_with_children()person1.set_age(25)如果 person1.age >21:person1.__class__ = 成人person1.say_hi()person1.drink_alcohol()
所以怪物:
类狗:def wouf(自我):打印(wouf")person1.__class__ = 狗print("属性已保存,名称:"+ person1.name)#但是方法不行,所以现在我不能使用person1.say_hi()person1.wouf()
I have a class Person
and 2 classes Child
and Adult
that inherit from `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")
I want to create an instance from Person
then set it's age
and according to it this instance should "become" a Child
or an Adult
.
Also if it's a Child
and it's age
increases I'd like it to "become" an Adult
.
Note that this code has an error because the Person
's constructor expects a name
because it's meant to create a new instance.
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()
Is this even possible?
Can this be done without creating new instances?
PS: this is just a sample code showing the problem from a more complex problem that I have (Not really children and adults :D ).
As mentioned in the comments, in Python 3 we can access the __class__
of any instance and change it.
Although you must be careful, with this you can add attributes of several classes to the same instance and create a weird monster (methods are safe).
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()
So the monster:
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()
这篇关于在python中将实例类型更改为继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!