如何在sub_type中使用super_type中的过程?

时间:2019-04-13 10:11:14

标签: fortran gfortran

如果我在super_type中有该代码,我想避免在sub_type中编写用于计算的代码。 这是我的测试代码:

module types

  type :: supertype
     integer, private :: a1
    contains
     procedure, public :: calc_data => calc_data_a1
  end type supertype

  private :: calc_data_a1 

  type, extends(supertype) :: subtype
     integer :: a2
    contains
     procedure, public :: calc_data => calc_data_a2
  end type subtype

  private :: calc_data_a2

contains

  integer function calc_data_a1(this)
   class(supertype), intent(inout) :: this
   this%a1 = 1 + 2
  end function calc_data_a1

  integer function calc_data_a2(this)
   class(subtype), intent(inout) :: this
   this%a1 = 1 + 2 !!! How to avoid writing this part of code which is same code from calc_data_a1 ???
   this%a2 = this%a1 + 3
  end function calc_data_a2

end module types

program types_pro
  implicit none
    type(subtype) :: type_obj
    type_obj%calc_data()
end program types_pro 

是否可以通过type_obj%calc_data()自动调用口程序[calc_data_a1calc_data_a2]以避免在两个地方编写相同的代码? Fortran中还有其他方法可以完成我提到的事情吗?

0 个答案:

没有答案