oop - Fortran 派生类型继承
问题描述
假设我有一个派生类型,它作为变量bar_a
包含在派生类型中。
现在我想扩展并创建一个名为. 我尝试了以下方法:foo_a
bar
bar_a
bar_b
program main
implicit none
! Base types -----------
type :: bar_a
integer :: a
end type bar_a
type :: foo_a
type(bar_a) :: bar
end type foo_a
! Extended types -------
type, extends(bar_a) :: bar_b
integer :: b
end type bar_b
type, extends(foo_a) :: foo_b
type(bar_b) :: bar ! <-- Component ‘bar’ at (1) already in the parent type
end type foo_b
! ----------------------
type(foo_b) :: foo
print *, foo%bar%a
print *, foo%bar%b
end program main
但我得到一个编译器错误:“(1)处的组件'bar'已经在父类型中”。
有没有办法扩展foo_a
,以便它包含bar_b
我尝试过的新派生类型,或者有什么方法可以“覆盖”bar
变量声明?我想继承将成为foo_a
in一部分的类型绑定过程foo_b
。
解决方案
当我尝试编译时,我得到了更好的消息:
aa.f90:21:22:
10 | type :: foo_a
| 2
......
21 | type(bar_b) :: bar ! <-- Component ‘bar’ already in the parent type
| 1
Error: Component ‘bar’ at (1) already in the parent type at (2)
这似乎是合乎逻辑的,您尝试foo_a
使用名称为 的元素进行扩展bar
,但是您扩展的类型(来自第 10 行的定义)在第 11 行已经有一个变量bar
,您尝试bar
在第 21 行添加另一个变量。
推荐阅读
- codeigniter - Codeigniter 按价格分页排序从第 2 页开始不起作用
- mysql - SQLAlchemy 相交错误
- vba - Excel 在复制/粘贴宏上冻结大约 10 分钟,更有效的方法是什么?
- python - 元组的 cerberus 模式验证器
- python - 使用 django 作为服务器时连续监听传感器
- tensorflow - tf.cast 的一个奇怪错误
- python - Keras 自定义损失函数,使用隐藏层输出作为目标的一部分
- python - tkinter treeview 行显示值与下划线的差异
- ruby-on-rails - 根据用户输入字段使用carrierwave重命名上传到s3的文件名
- javascript - JavaScript 深度嵌套数组过滤