Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 如何根据多态变量的数据类型指定要执行的过程_Oop_Polymorphism_Fortran_Fortran2003 - Fatal编程技术网

Oop 如何根据多态变量的数据类型指定要执行的过程

Oop 如何根据多态变量的数据类型指定要执行的过程,oop,polymorphism,fortran,fortran2003,Oop,Polymorphism,Fortran,Fortran2003,考虑以下示例代码: module mod implicit none type :: typeBase1 integer :: A1 end type type :: typeBase2 integer :: A3 end type type :: typeBase3 integer :: A3 end type type, extends(typeBase1) :: typeDerived1 ! Void end type type, extends

考虑以下示例代码:

module mod

implicit none

type :: typeBase1
    integer :: A1
end type

type :: typeBase2
    integer :: A3
end type

type :: typeBase3
    integer :: A3
end type

type, extends(typeBase1) :: typeDerived1
    ! Void
end type

type, extends(typeBase2) :: typeDerived2
    ! Void
end type

type, extends(typeBase3) :: typeDerived3
    ! Void
end type

type, extends(typeBase1) :: typeDerived11        
    ! Void
end type

type, extends(typeBase2) :: typeDerived21
    ! Void
end type

type, extends(typeBase3) :: typeDerived31
    ! Void
end type


type :: complexType
   class(typeBase1), pointer :: ptrBase1  ! typeBase1, 2 and 3 are extensible
   class(typeBase2), pointer :: ptrBase2
   class(typeBase3), pointer :: ptrBase3
end type

interface calcul
    subroutine calculA(obj1, obj2, obj3)
        import
        type(typeDerived1) :: obj1        ! typeDerived 1, 2 et 3 are derived type of typeBase1, 2 and 3 
        type(typeDerived2) :: obj2
        type(typeDerived3) :: obj3
    end subroutine
    subroutine calculB(obj1, obj2, obj3)
        import
        type(typeDerived11) :: obj1        ! typeDerived 11, 21 et 31 are derived type of typeBase1, 2 and 3 
        type(typeDerived21) :: obj2
        type(typeDerived31) :: obj3
    end subroutine
end interface calcul

contains

    subroutine calculComplexType(complex)
        type(ComplexType), intent(inout) :: complex

        call calcul(complex % ptrBase1, complex % ptrBase2, complex % ptrBase3)
    end subroutine

end module mod
我想做的是,子程序CalcultComplexType根据动态类型ptrBase1、ptrBase2和ptrBase3调用子程序calcul的不同版本

代码不起作用,因为编译器查找具有以下接口的子例程:

subroutine calcul(obj1, obj2, obj3)
    class(typeBase1) :: obj1         
    class(typeBase1) :: obj2
    class(typeBase1) :: obj3
end subroutine
无论ptrBase1、ptrBase2和ptrBase3的动态类型是什么

我的问题是:在Fortran中是否有一种方法可以编写接口计算,以便根据参数的动态类型自动选择一个过程

我希望避免使用“选择类”的长序列


欢迎任何重写代码的建议

如果您基于这三个参数请求分派,则无法执行。有些语言为此提供所谓的服务

在Fortran中,您可以使用普通的单分派方法(),但在这种情况下,它只能根据一个参数选择子例程

否则,您必须使用选择构造并为每个可能的组合制作一个
案例
,无论是在单个过程中还是在多个版本之间进行选择


对于两个参数,您也可以考虑.< /P> < P>这在Fortran根本不可能;使用多态性最好的方法是使用重写的类型绑定过程,根据一个特定实体的动态类型选择函数

然而,根据
calcul
的性质,只定义一个采用多态参数的
calcul
版本(即
class(typeBase1)
class(typeBase2)
class(typeBase3)
)可能更有意义,并处理
calcul
本身内部的动态类型。好处有两方面:

  • calcul
    可以独立于其他参数测试每个参数的类型。如果是这种情况,您仍然需要编写三个
    selecttype
    构造,但它们不会嵌套或重复

  • 您很可能可以使用单一分派(带有类型绑定过程)来完全消除对
    select type
    构造的需要

  • 我很难想象这个问题中的代码是您可以使用的最佳设计

    如果
    calcul
    确实在为每个动态类型执行与调用它的代码相关的完全“不同”的操作,那么调用代码不应该使用多态指针(例如,对于每个不同的
    calcul
    ,可能应该有不同的
    complexType


    但是,如果每个版本的
    calcul
    都在执行本质上“相同”的操作(就高级代码所知/所关心的而言),无论动态类型如何,然后应该只有一个版本,并且它应该接受基类的参数。

    可能重复类型绑定过程的诡辩-它只能根据call语句或函数引用中过程指示符中绑定名称左侧的对象选择子例程。只有一个这样的对象,因此不能在三个对象上一步分派。用于分派的对象通常作为第一个参数传递,但它可以是任何其他参数,甚至可以根本不传递。OP可以用它来实现一个三步调度,但我认为他们仍然需要大量的输入。这只是一个小小的简化。当然,您可以使用“类方法”。