英文:
I'm trying to extend a class but exclude a certain property in the process
问题
I have a class Food and I extended it to a class OrderedItem but I'm trying to extend it without the num score property. Is this possible?
我有一个名为 Food 的类,我将它扩展成了一个名为 OrderedItem 的类,但我想在新类中去掉 num score 属性。这是否可能?
英文:
I have a class Food and I extended it to a class OrderedItem but I'm trying to extend it without the num score property. Is this possible?
Food {
String imgUrl;
String desc;
String name;
String waitTime;
num score;
int price;
int pq;
int quantity;
bool favourited;
List<Variation> variations;
Variation selectedVar;
String about;
bool highlight;
}
OrderedItem
class OrderedItem extends Food {
Variation selectVar;
OrderedItem(
this.selectVar,
super.imgUrl,
super.desc,
super.name,
super.pq,
super.waitTime,
super.score, //I'm trying to remove this in the new class
super.price,
super.quantity,
super.variations,
super.about,
super.favourited,
{super.highlight = false});
}
答案1
得分: 1
不,不可能“排除”一个属性。
您应该重新构建您的继承链:
Food 不应该在第一次包含一个分数属性。
从食物中删除 score 属性。
引入一个新的类 ScoredFood,它扩展了 Food 并添加了分数属性。
现在 OrderedItem 可以扩展 Food,而不继承分数属性。
此外,请重新考虑在这里是否应该使用继承或组合。
一个 OrderedItem 真的是一个 Food 吗,还是一个 Food 只是一个 OrderedItem 的一部分(因此应该作为其 food 属性可访问)?
英文:
No, it is not possible to "exclude" a property.
You should instead refactor your inheritance chain:
Food should not include a score property in the first place.
Remove the score property from food.
Introduce a new class ScoredFood which extends Food and adds the score property.
Now OrderedItem can extend Food without inheriting a score property.
Besides, reconsider whether you should be using inheritance or composition here.
Is an OrderedItem really a Food, or is a Food just part of an OrderedItem (and should thus be accessible as a food property of it)?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论