gpt4 book ai didi

python - 属性和从实例继承

转载 作者:太空宇宙 更新时间:2023-11-04 10:44:47 24 4
gpt4 key购买 nike

所以我通常不使用 OOP,显然我对它的理解并不像我认为的那样。

假设我有一个(地理)状态的类:

class State(object):
@property
def population(self):
return self._population
@population.setter
def population(self,value):
if value < 0:
raise ValueError("Population must not be negative")
else:
self._population = value

和一个几乎相同的(目前)城镇类:

class Town(State):
@property
def population(self):
return self._population
@population.setter
def population(self,value):
if value < 0:
raise ValueError("Population must not be negative")
else:
self._population = value

现在假设我实例化一个州并给它一个特定的人口。我将如何创建一个继承该州实例人口的城镇实例? (我想暂时 - 这只是一个例子。)或者我应该使用组合而不是继承?

按照我目前的想法,这应该可行:

s = State()

s.population = 10

t = Town(s)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-141-00f052d998f0> in <module>()
----> 1 t = Town(s)

TypeError: object.__new__() takes no parameters

最佳答案

你不会说一个城镇是一个州(除非你住在新加坡或香港!)。但是你会说一个州有一个城镇。这表明组成。

请注意,默认情况下,州甚至没有人口属性。所以继承State并没有赋予Town任何属性:

class State(object):
@property
def population(self):
return self._population
@population.setter
def population(self,value):
if value < 0:
raise ValueError("Population must not be negative")
else:
self._population = value

s = State()
print s.population

--output:--
raceback (most recent call last):
File "1.py", line 13, in <module>
print s.population
File "1.py", line 4, in population
return self._population
AttributeError: 'State' object has no attribute '_population'

所以当你说:

Now suppose I instantiate a State and give it a specific population. How would I create an instance of a Town that inherits that State instance's population?

...这没有任何意义,因为 Town 类不知道任何 State 实例。为 Town 实例提供与 State 实例相同人口的显而易见的答案是:

class State(object):
@property
def population(self):
return self._population
@population.setter
def population(self,value):
if value < 0:
raise ValueError("Population must not be negative")
else:
self._population = value

class Town(object):
def __init__(self, population):
self._population = population

@property
def population(self):
return self._population

s = State()
s.population = 30
print s.population

t = Town(s.population)
print t.population

使用组合,你可以做这样的事情:

class State(object):
def __init__(self, name, *towns):
self.name = name
self.towns = towns

@property
def population(self):
total = 0
for town in self.towns:
total += town.population
return total

class Town(object):
def __init__(self, name, population):
self._population = population

@property
def population(self):
return self._population

@population.setter
def population(self,value):
if value < 0:
raise ValueError("Population must not be negative")
else:
self._population = value

detroit = Town("Detroit", 40)
lansing = Town("Lansing", 100)
detroit.population -= 10
print detroit.population
print lansing.population

s = State("Michigan", detroit, lansing)
print s.population

--output:--
30
100
130

关于python - 属性和从实例继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18198605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com