首页 > 解决方案 > Fortran 扩展类型重载过程,但使用父过程

问题描述

我想知道是否有可能有一个扩展派生类型重载一个过程但仍然设法调用“父”过程。

类似的东西(不起作用,无限递归)

module parent_class
  implicit none
  private
    type,public,abstract :: parent
  contains
    procedure,pass(self) :: sub
  end type
contains
  subroutine sub(self)
    class(parent),intent(inout) :: self

    ! Do something here
    write(*,*) 'in parent'
  end subroutine
end module

module child_class
  use parent_class, only: parent
  implicit none
  private
    type,public,extends(parent) :: child
  contains
    procedure,pass(self) :: sub
  end type
contains
  subroutine sub(self)
    class(child),intent(inout) :: self

    ! do something here
    ! ... 
    write(*,*) 'in child'

    call self%sub() ! < this is the parent procedure
  end subroutine
end module

program main
  use child_class
  implicit none

  type(child) :: my_child

  call my_child%sub()
end program

一种解决方案是在模块sub中公开可用并在parent_class模块中使用它,child_class但我想知道其他解决方案。

标签: fortranpolymorphismoverloading

解决方案


推荐阅读