【问题标题】:Polymorphism in type bound procedures类型绑定过程中的多态性
【发布时间】:2014-04-10 11:55:38
【问题描述】:

有没有办法使用与类型绑定过程相同的函数来处理两种或多种类型?例如。想象以下情况:

module definitions
implicit none

type type1
   integer i
   contains
   procedure,pass :: init1
end type

type type2
   integer i
   contains
   procedure,pass :: init2
end type

contains

subroutine init1(x)
   class(type1),intent(inout) :: x
   x%i=3
end subroutine

subroutine init2(x)
   class(type2),intent(inout) :: x
   x%i=3
end subroutine

end module

program test
use definitions

type(type1) ::a
type(type2) ::b

print*, a%i
print*, b%i
call a%init1
print*, a%i
print*, b%i
call b%init2
print*, a%i
print*, b%i

end program

如您所见,我使用了相同的子例程,但我觉得不得不定义它两次。所以我要求类似的东西

class(type1 .or. type2), intent(inout) :: x

或类似的。我已经尝试过 class(*) 但这当然不起作用,因为编译器不知道如何处理可能未定义的 x%i,即使不与选择类型块结合使用。我想提一下,真正的程序更复杂,因此将类型定义的相似部分合并然后扩展以定义这两种类型并不容易。 提前致谢!

【问题讨论】:

    标签: fortran derived-class


    【解决方案1】:

    这是不可能的。但是为什么不创建一个基类型然后扩展呢?

    module definitions
    implicit none
    
    type base
       integer i
    contains
       procedure,pass :: init
    end type
    
    
    type, extends(base) :: type1
    end type
    
    type, extends(base) ::  type2
    end type
    
    contains
    
    subroutine init(x)
       class(base),intent(inout) :: x
       x%i=3
    end subroutine
    
    end module
    
    program test
    use definitions
    
    type(type1) ::a
    type(type2) ::b
    
    print*, a%i
    print*, b%i
    call a%init
    print*, a%i
    print*, b%i
    call b%init
    print*, a%i
    print*, b%i
    
    end program
    

    编辑(PM):这解决了我真正想到的问题:

    module definitions
    implicit none
    
    type base
    integer :: i
    contains
       procedure,pass :: init
    end type
    
    type, extends(base) :: type1
    integer:: j
    end type
    
    type, extends(base) ::  type2
    integer:: k
    end type
    
    contains
    
    subroutine init(x)
       class(base),intent(inout) :: x
       integer :: m
       select type (x)
       type is (type1)
       m=x%j
       type is (type2)
       m=x%k
       end select
       x%i=3*m
    end subroutine
    
    end module
    
    program test
    use definitions
    
    type(type1) ::a
    type(type2) ::b
    
    a%j=2
    b%k=4
    print*, a%i
    print*, b%i
    call a%init
    print*, a%i
    print*, b%i
    call b%init
    print*, a%i
    print*, b%i
    
    end program
                                                                                                                                                       1,5           Top
    

    【讨论】:

    • 是的,这确实解决了示例中的问题,但实际上我试图用我的最后一句话排除这个解决方案。由于在实际程序中,该功能将依赖于扩展。很抱歉造成这种混乱。尽管如此,它仍然像那样工作,略有变化。我会相应地编辑您的帖子并将其标记为答案。
    • 这很奇怪。如果您真的在使用两个不同的组件,只有一个孩子存在,您可能确实应该使用 2 个单独的程序。如果有一些共享部分,你也可以随时调用 base 的过程。
    • 这些组件具有相同的结构,但也依赖于整个子组件的其他组件。但最后我需要对他们应用相同的公式。顺便说一句,这在物理意义上也是令人惊讶的,这是所有这一切的背后。但我真的无法在这里描述细节。现在似乎仍然工作得很好。如果您预计我的编辑中有任何“隐藏”问题,我当然非常感谢您了解它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    相关资源
    最近更新 更多