【发布时间】:2021-02-01 18:06:40
【问题描述】:
假设我有一个派生类型 bar_a,它作为变量 bar 包含在派生类型 foo_a 中。
现在我想扩展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_b 中继承将成为foo_a 一部分的类型绑定过程。
【问题讨论】:
标签: oop fortran derived-types