class Fruit: def __init__(self,color="绿色"): Fruit.color=color def harvest(self): print("水果原来是:"+Fruit.color+"的!") class Apple(Fruit): def __init__(self): print("我是苹果") apple=Apple() apple.harvest()让他不报错
时间: 2024-03-02 17:47:51 浏览: 22
这段代码存在一个问题,即在子类 Apple 中没有调用父类 Fruit 的构造函数。因此,需要在子类的构造函数中显式地调用父类的构造函数,即使用 super() 函数。修改后的代码如下:
```
class Fruit:
def __init__(self, color="绿色"):
Fruit.color = color
def harvest(self):
print("水果原来是:" + Fruit.color + "的!")
class Apple(Fruit):
def __init__(self):
super().__init__()
print("我是苹果")
apple = Apple()
apple.harvest()
```
运行后输出:
```
我是苹果
水果原来是:绿色的!
```
相关问题
def test_mobilenet(): # todo 加载数据, 224*224的大小 模型一次训练16张图片 train_ds, test_ds, class_names = data_load(r"C:\Users\wjx\Desktop\项目\data\flower_photos_split\train", r"C:\Users\wjx\Desktop\项目\data\flower_photos_split\test", 224, 224, 16) # todo 加载模型 model = tf.keras.models.load_model("models/mobilenet_fv.h5") # model.summary() # 测试,evaluate的输出结果是验证集的损失值和准确率 loss, accuracy = model.evaluate(test_ds) # 输出结果 print('Mobilenet test accuracy :', accuracy) test_real_labels = [] test_pre_labels = [] for test_batch_images, test_batch_labels in test_ds: test_batch_labels = test_batch_labels.numpy() test_batch_pres = model.predict(test_batch_images) # print(test_batch_pres) test_batch_labels_max = np.argmax(test_batch_labels, axis=1) test_batch_pres_max = np.argmax(test_batch_pres, axis=1) # print(test_batch_labels_max) # print(test_batch_pres_max) # 将推理对应的标签取出 for i in test_batch_labels_max: test_real_labels.append(i) for i in test_batch_pres_max: test_pre_labels.append(i) # break # print(test_real_labels) # print(test_pre_labels) class_names_length = len(class_names) heat_maps = np.zeros((class_names_length, class_names_length)) for test_real_label, test_pre_label in zip(test_real_labels, test_pre_labels): heat_maps[test_real_label][test_pre_label] = heat_maps[test_real_label][test_pre_label] + 1 print(heat_maps) heat_maps_sum = np.sum(heat_maps, axis=1).reshape(-1, 1) # print(heat_maps_sum) print() heat_maps_float = heat_maps / heat_maps_sum print(heat_maps_float) # title, x_labels, y_labels, harvest show_heatmaps(title="heatmap", x_labels=class_names, y_labels=class_names, harvest=heat_maps_float, save_name="images/heatmap_mobilenet.png")
这段代码是用来测试 Mobilenet 模型在花卉数据集上的表现的。首先,使用 `data_load` 函数加载数据集,然后使用 `tf.keras.models.load_model` 函数加载预训练好的 Mobilenet 模型。接着,使用 `model.evaluate` 函数对测试集进行评估,得到测试集的损失值和准确率。然后,依次对测试集中的每一批数据进行预测,将真实标签和预测标签分别存储在两个列表中。最后,使用这两个列表生成混淆矩阵,并将混淆矩阵可视化为热力图。
4-1有一个水果(fruit)类。有水果名称(name), 重量(weight)和颜色(color),能够颜色等属性信息。在水果显示类上派生苹果(apple)(苹果水果的名称、重量、名称指水果的名称梨名称指定为Pear)两个类,Apple),梨(pear)(派生类中添加品种属性信息,能够显在、品种、重定为示量、颜色等属性信息。要求有构造方法能对数据成员进行初始化。 (知识点:继承,重写) 拓展:可以为水果添加品种,采摘日期,保质期等属性信息,可以查询是否在保质期内。
好的,这是一个关于继承和重写的编程问题。下面是我的回答:
可以先定义一个基类 Fruit,包含名称、重量和颜色等属性信息,然后派生出 Apple 和 Pear 两个子类,其中 Pear 类还添加了品种属性信息。
代码示例:
```python
class Fruit:
def __init__(self, name, weight, color):
self.name = name
self.weight = weight
self.color = color
class Apple(Fruit):
def __init__(self, name, weight, color):
super().__init__(name, weight, color)
class Pear(Fruit):
def __init__(self, name, weight, color, variety):
super().__init__(name, weight, color)
self.variety = variety
```
如果要拓展属性信息,可以在 Fruit 类中添加相应的属性,然后在子类中进行初始化和查询操作。
代码示例:
```python
class Fruit:
def __init__(self, name, weight, color, variety=None, harvest_date=None, shelf_life=None):
self.name = name
self.weight = weight
self.color = color
self.variety = variety
self.harvest_date = harvest_date
self.shelf_life = shelf_life
def is_fresh(self):
# 判断是否在保质期内
if self.harvest_date and self.shelf_life:
# 假设保质期为30天
days_since_harvest = (datetime.now() - self.harvest_date).days
if days_since_harvest <= self.shelf_life:
return True
return False
class Apple(Fruit):
def __init__(self, name, weight, color, variety=None, harvest_date=None, shelf_life=None):
super().__init__(name, weight, color, variety, harvest_date, shelf_life)
class Pear(Fruit):
def __init__(self, name, weight, color, variety, harvest_date=None, shelf_life=None):
super().__init__(name, weight, color, variety, harvest_date, shelf_life)
```
这样,我们就可以通过 is_fresh 方法来查询水果是否在保质期内了。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)