【问题标题】:"Extending" a function in a Fortran derived type在 Fortran 派生类型中“扩展”函数
【发布时间】:2018-04-25 22:24:04
【问题描述】:

我说“扩展”是因为,原谅我,我不确定这里正确的 OOP 术语是什么。我并不完全想覆盖一个函数。我希望继承派生类型中的函数完成同名函数在父类型中所做的所有工作,然后添加到该函数中。我想要类似的东西:

module foo1
  type :: bar1
  contains
    procedure :: f1
  end type bar1
contains
  subroutine bar1()
  ! do stuff
  end subroutine bar1
end module foo1

module foo2
  use foo1
  type, extends(bar1) :: bar2
  contains
    procedure :: f1
  end type bar2
contains
  subroutine f1()
    ! call parent f1
    ! do other stuff
  end subroutine f1
end module foo2

有没有办法在 Fortran 中做到这一点?

【问题讨论】:

标签: inheritance fortran derived-types


【解决方案1】:

您必须进行正常的覆盖,并且在新过程中您必须手动调用父类型过程(通过 ptb A Fortran analog to python's super()? 在评论中链接的方式)。

subroutine f1(self)
  call self%bar1%f1 
  !the rest
end subroutine

【讨论】:

  • 正是我所需要的。谢谢!
猜你喜欢
  • 2012-10-23
  • 2019-03-02
  • 2020-07-20
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
相关资源
最近更新 更多