为什么我的class属性高度对于不同的实例返回相同的值?
th.mat-header-cell,
td.mat-cell {
text-align: center !important;
}
当我尝试将值添加到高度列表时,另一个实例也将添加该值。我可以通过以下方法解决此问题:
class Tree:
def __init__(self, age, tree_type, heights=[]):
self.age = age
self.tree_type = tree_type.title()
self.heights = heights
def __str__(self):
return (f"I am {self.age} and {self.tree_type}. My heights are: {' '.join(self.heights)}")
def add_height(self,height):
self.heights += [height]
self.age += 1
print(f"Added height of {self.heights[-1]} to {self.tree_type} tree.")
oak_tree = Tree(1, 'oak')
birch_tree = Tree(2, 'Birch')
print(oak_tree)
print(birch_tree)
oak_tree.add_height('5m')
birch_tree.add_height('10m')
print(oak_tree)
print(birch_tree)
#output
I am 1 and Oak. My heights are:
I am enter code here2 and Birch. My heights are:
Added height of 5m to Oak tree.
Added height of 10m to Birch tree.
I am 2 and Oak. My heights are: 5m 10m
I am 3 and Birch. My heights are: 5m 10m
但是我还是很困惑。我认为,如果没有传递任何值,则可以在属性后使用=符号声明变量。我不能使用列表吗?