我正在使用一些新功能更新一些代码,理想情况下,我的解决方案将包括一个数组,其中每个元素都具有不同的类型,这在fortran中是不可能的。因此,我尝试使用多态对象数组,但被卡在初始化数组元素之后似乎无法调用任何类型绑定子例程的地方。
这是问题的要点。
类型声明模块:
type :: fruit
end type fruit
type, extends(fruit) :: apples
contains
procedure :: init => init_apples
end type apples
type, extends(fruit) :: oranges
contains
procedure :: init => init_oranges
end type oranges
contains
pure subroutine init_apples(me)
class(apples), intent(inout) :: me
! Do Stuff
end subroutine init_appples
pure subroutine init_oranges(me)
class(oranges), intent(inout) :: me
! Do Stuff
end subroutine init_oranges
然后在主程序中:
use apropriate module
type fruit_basket
class(fruit), allocatable :: item
end type
type(fruit_basket), allocatable :: gift(:)
allocate(gift(2))
!Option 1
allocate(apples::gift(1).item)
allocate(oranges::gift(2).item)
!Option 2
gift(1) = fruit_basket(apples)
gift(2) = fruit_basket(oranges)
编译器接受以上任一选项。现在,理想情况下,我想将这些元素之一称为init
!Tried this
call gift(1).init()
!Also tried this
call gift(1).item.init()
这两个都会产生错误: 错误#6460:这不是在包含结构中定义的字段名称。 [init]
我在做什么错了?
编辑:
所以我有点喜欢它工作,对它的工作方式并不满意,但是我想它会做:
类型声明:
Module type_declaration
implicit none
type, abstract, public :: Base_Type
contains
procedure(init_base), deferred :: init
procedure(get_base), deferred :: GetResult
end type
interface
pure subroutine init_base(this, x)
import Base_Type
class(base_type), intent(inout) :: this
real(8), intent(in) :: x
end subroutine
real(8) pure function get_base(this)
import Base_Type
class(base_type), intent(in) :: this
end function
end interface
type, extends(Base_Type) :: Subtype1
real(8) alpha
contains
procedure :: init => init_type1
procedure :: GetResult => get_res_type1
end type
type, extends(Base_Type) :: Subtype2
real(8) alpha, beta
contains
procedure :: init => init_type2
procedure :: GetResult => get_res_type2
end type
contains
pure subroutine init_type1(this, x)
class(Subtype1), intent(inout) :: this
real(8), intent(in) :: x
!Work here
this.alpha = x * 2.
end subroutine
pure subroutine init_type2(this, x)
class(Subtype2), intent(inout) :: this
real(8), intent(in) :: x
!Work here
this.alpha = x * 2
this.beta = x / 3.
end subroutine
real(8) pure function get_res_type1(this)
class(Subtype1), intent(in) :: this
get_res_type1 = this.alpha
end function
real(8) pure function get_res_type2(this)
class(Subtype2), intent(in) :: this
get_res_type2 = this.alpha + this.beta
end function
end module type_declaration
主程序变体1:
program Polymorhic_Test
use type_declaration
implicit none
type data_container
class(Base_type), allocatable :: item
end type
type(data_container) :: MainArray(2)
real(8) :: x, y = 0.
character(10) :: cDummy
allocate(Subtype1::MainArray(1).item)
allocate(Subtype2::MainArray(2).item)
x = 1.
call MainArray(1).item.init(x)
y = MainArray(1).item.GetResult()
x = 2.
call MainArray(2).item.init(x)
y = MainArray(2).item.GetResult()
read cDummy
end program Polymorhic_Test
主程序变体2
program Polymorhic_Test
use type_declaration
implicit none
type data_container
class(Base_type), allocatable :: item
end type
type(data_container) :: MainArray(2)
real(8) :: x, y = 0.
character(10) :: cDummy
type(Subtype1) :: ST1
type(Subtype2) :: ST2
x = 1.
call ST1.init(x)
allocate(MainArray(1).item, source=ST1)
y = MainArray(1).item.GetResult()
x = 2.
call ST2.init(x)
allocate(MainArray(2).item, source=ST2)
y = MainArray(2).item.GetResult()
read cDummy
end program Polymorhic_Test
我可能会选择选项2,只是因为我然后不必推迟基本类型的初始化,因为我的子类型将具有不同数量的初始化所需的参数,而且我不喜欢拥有基本类型的想法输入带有20个可选参数的init。