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 fortran中的nopass程序与普通函数_Oop_Static_Fortran - Fatal编程技术网

Oop fortran中的nopass程序与普通函数

Oop fortran中的nopass程序与普通函数,oop,static,fortran,Oop,Static,Fortran,因此,在其他语言中,静态方法可以访问静态成员,并且它们的可见性受到类范围的限制。在fortran中,没有静态成员(如果我错了,请纠正我),并且方法名是全局可访问的,因此我甚至不能在不同的类中有两个同名的静态方法。我认为“NOPAS”方法是“静态”的,但我甚至不确定这个术语是否适用。综上所述,我看不出与普通模块函数有什么区别。与普通函数相比,使用nopass方法有什么好处吗 编辑: 无法在不同类中拥有两个同名静态方法的示例: module test_mod type type1 conta

因此,在其他语言中,静态方法可以访问静态成员,并且它们的可见性受到类范围的限制。在fortran中,没有静态成员(如果我错了,请纠正我),并且方法名是全局可访问的,因此我甚至不能在不同的类中有两个同名的静态方法。我认为“NOPAS”方法是“静态”的,但我甚至不确定这个术语是否适用。综上所述,我看不出与普通模块函数有什么区别。与普通函数相比,使用nopass方法有什么好处吗

编辑:

无法在不同类中拥有两个同名静态方法的示例:

module test_mod
  type type1
  contains
    procedure, nopass :: proc => proc1
  end type

  type type2
  contains
    procedure, nopass :: proc => proc2
  end type

contains
  subroutine proc1()
    print *, 'proc1'
  end subroutine

  subroutine proc2()
    print *, 'proc2'
  end subroutine
end module

显然,我现在不能只说
call proc()
,也不能使用类名来帮助编译器选择正确的方法。

考虑可访问性:只要可以访问类型,就可以访问公共类型绑定过程

module mod
  private
  type, public :: type1
  contains
    procedure, nopass :: proc
  end type

contains

  subroutine proc
  end subroutine

end module

  use mod
  type(type1) t1

  call t1%proc
  call proc  ! No, we can't call proc from mod1

end
类似地,使用
时,仅使用

我们可以避免模块过程名称中的冲突,而无需在使用关联时重命名:

  use mod1, only : type1
  use mod2, only : type2

  type(type1) t1
  type(type2) t2

  call t1%proc
  call t2%proc    ! These aren't ambiguous

end
为了避免程序上的模糊性,我们不得不重命名:

  use mod1, proc1=>proc
  use mod2, proc2=>proc

  call proc1
  call proc2

end
还有程序参考的动态选择:

module mod
  type type1
  contains
    procedure, nopass :: proc=>proc1
  end type

  type, extends(type1) :: type2
  contains
    procedure, nopass :: proc=>proc2
  end type

contains

  subroutine proc1
  end subroutine

  subroutine proc2
  end subroutine

end module

use mod
class(type1), allocatable :: t

t=type1()
call t%proc  ! proc1

t=type2()
call t%proc  ! proc2

end
但是,应该注意的是,像
t1%proc
这样的绑定名与过程名不同

 use mod
 type(type1) t1
 call sub(proc1)    ! Where this is accessible
 call sub(t1%proc)  ! We cannot do this

contains

 subroutine sub(proc)
   procedure() proc
 end subroutine

end

你说“我甚至不能在不同的类中有两个同名的静态方法”是什么意思?@francescalus我已经更新了我的问题,你在任何情况下都不能调用proc<代码>调用t1%proc另一方面(对于
类型(type1)t1
)…@francescalus好的,我不知道有人可以这样做。以前,我只是给它们起不同的名字,不使用对象限定符来调用它们,就像普通函数一样。所以nopass方法的第一个优点是限制名称的可见性,然而,这是以拥有一个虚拟的未初始化对象为代价的,该对象至少不优雅。还有其他好处吗?
 use mod
 type(type1) t1
 call sub(proc1)    ! Where this is accessible
 call sub(t1%proc)  ! We cannot do this

contains

 subroutine sub(proc)
   procedure() proc
 end subroutine

end