因此,我介紹了python類,我們剛剛進入python類的basses編碼。我希望從列表中的植物中得到一個區域列表,以便我可以拿走這些區域,獲得一筆總額,然後從牀上的區域中扣除它們。我很難弄清楚如何製作我牀上植物區域的清單,這個清單現在只是藍莓。任何想法,不勝感激。如何獲得基於類的基礎系統的區域列表
class crop:
name = "Crop"
sizeW = 0
sizeL = 0
areaOfCrop=sizeL * sizeW
def print(self):
print("This is a ",self.name," and is ",self.sizeW,"X",self.sizeL, "and has an area of ",self.areaOfCrop)
def willItFit(self, W, L):
return self.sizeW <= W and self.sizeL <= L
def sameAs(self, crop):
return self.name == crop.name
class plant(crop):
name = "Plant"
sizeW = 1
sizeL = 1
areaOfCrop = sizeL * sizeW
class parsley(plant):
name = "Parsley"
class shrub(crop):
name = "Shrub"
sizeW = 2
sizeL = 2
areaOfCrop = sizeL * sizeW
class blueberries(shrub):
name = "Blueberries"
class tree(crop):
name = "tree"
sizeW = 3
sizeL = 3
areaOfCrop = sizeL * sizeW
class dwarfPeach(tree):
name = "Dwarf Peach"
class bed:
sizeL = int(input("What is the length? "))
sizeW = int(input("What is the width? "))
crops = [blueberries()] # The list of crops in this bed
areaOfAllCrops = [crops[areaOfCrop()]] #this is where my problem is
def print(self):
print("The bed is ", self.sizeL," x ", self.sizeW)
for c in self.crops:
c.print()
def maxSizeAvailable(self):
''' area of bed-total area of all crops in be '''
return (self.sizeW, self.sizeL)
def add(self, newCrop):
dimension = self.maxSizeAvailable()
if newCrop.willItFit(dimension[0], dimension[1]):
self.crops.append(newCrop)
def remove(self, existingCrop):
for c in self.crops:
if c.sameAs(existingCrop):
self.crops.remove(c)
def checkFit(self, crop):
dimension = self.maxSizeAvailable()
return crop.willItFit(dimension[0], dimension[1])
b = bed()
b.print()
非常感謝你給這個提示!如果它有效。我不確定它是否有幫助。教授給了我們大部分的代碼,並告訴我們自己想出最後一部分。你所建議的是這樣的方式,但他讓我們這樣做來學習基礎知識。 –