T_E*_*M_A 3 delphi constructor properties object
我尝试这样做,但在构造函数中出错。
E2029 'OF' 预期但发现 '['
Dish = class
public
Dish_name: string;
Dish_id: integer;
Dish_number: integer;
Dish_units: string;
Dish_price: integer;
Dish_prime_cost: integer;
Dish_ingredients_id: array[1..100] of Ingredients;
constructor Create( NewDish_name: string;
NewDish_id: integer; NewDish_number: integer;
NewDish_units: string; NewDish_price: integer;
NewDish_prime_cost: integer;
NewDish_ingredients_id:array[1..100] of Ingredients);
destructor Destroy;
end;
Run Code Online (Sandbox Code Playgroud)
成分是一流的。
您不能将内联静态数组声明为参数。您需要声明一个类型:
type
TIngredientsArray = array[1..100] of Ingredients;
Run Code Online (Sandbox Code Playgroud)
然后对您的字段和参数使用该类型。
但是,静态大小的数组可能不是最佳选择。如果您需要 100 多种成分怎么办?如果您只需要 1 个,但被迫超过 100 个怎么办?就你的代码而言,菜类如何知道实际提供了多少配料?
考虑改用动态数组。
您还应该考虑这些成分对象的生命周期和所有权。您将对象传递给菜类。它是否承担所有权?也就是说,谁负责销毁所有的配料对象?如果您可以为成分使用值类型,即 a record,那么您可能会发现这使得生命周期管理更简单。
在 Delphi 中对数组使用从零开始的索引也是惯用的。动态数组是从零开始的。标准集合类是从零开始的。如果您开始使用基于一个的数组,那么经验表明您将来会存储混乱。