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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
Class fortran语言中的多态性_Class_Oop_Polymorphism_Fortran2003_Fortran2008 - Fatal编程技术网

Class fortran语言中的多态性

Class fortran语言中的多态性,class,oop,polymorphism,fortran2003,fortran2008,Class,Oop,Polymorphism,Fortran2003,Fortran2008,我的代码类似于: Module C_sys use class_A implicit none Private Type, public :: C_sys_type private logical :: Ao_set = .false. type(A) :: Ao Contains Private Procedure, public :: get_Ao Procedure, public :: set_Ao

我的代码类似于:

Module C_sys

  use class_A

   implicit none

    Private

    Type, public :: C_sys_type

  private

   logical   :: Ao_set = .false. 

   type(A) :: Ao

 Contains

 Private

    Procedure, public :: get_Ao
    Procedure, public :: set_Ao

 End Type C_sys_type

  interface C_sys_type

   Procedure C_sys_type_constructor

  end interface C_sys_type

 Contains

type(C_sys_type) elemental function C_sys_type_constructor(Ao) result(C_sys)

   type(A), intent(in), optional :: Ao

    C_sys % Ao = Ao
   C_sys % Ao_set = .true.

end function C_sys_type_constructor

type(A) elemental function get_Ao 
  class(C_sys_type), intent(in) :: this

   get_Ao = this % Ao
 end function get_Ao

 subroutine set_Ao(this, Ao)

 class(C_sys_type), intent(inout) :: this
 type(Ao), intent(in)             :: Ao

   this % Ao     = Ao  
   this % Ao_set = .true.

end subroutine set_Ao

End Module C_sys
我不确定在子例程集合_Ao的哪个位置,type(Ao),intent(in)::Ao应该这样保留,或者改为使用class(Ao),intent(in)::Ao。我知道类(Ao)正在使变量多态并访问A的数据类型。但我不知道何时必须使用它


谢谢。

如果您希望能够将函数/子例程绑定到派生类型(并且该例程能够访问/修改该类型实例的成员,这是常见的用例;称为“
传递
ing”变量),您需要满足以下条件:

  • 类型
    定义必须包含适当的
    过程
    行(明确说明
    通过
    ,或者默认情况下,只要未指定
    NOPASS
  • 函数/子例程至少有一个有问题的
    类型
    的伪参数,必须在参数列表中用
    声明(受所有必要限制的约束)。
    • 原因是它需要
      ,因为其他一些
      类型
      可能会“扩展”您的
      类型
      ,这意味着它会继承其成员-这只能在成员例程是数据多态的情况下工作
我试图将您提供的代码示例修改为一些我认为您实际上的意思的代表性内容,但实际上是编译的,希望能够演示正确的用法

module c_sys
  implicit none

  private

  type, public :: a
    integer :: i
  end type

  type, public :: c_sys_type
    private
    logical :: ao_set = .false.
    type(a) :: ao
  contains
    private
    procedure, public :: get_ao
    procedure, public :: set_ao
  end type c_sys_type

  interface c_sys_type
    procedure c_sys_type_constructor
  end interface c_sys_type

contains

  type(c_sys_type) elemental function c_sys_type_constructor(ao) result(c_sys)
    type(a), intent(in), optional :: ao

    c_sys % ao = ao
    c_sys % ao_set = .true.
  end function c_sys_type_constructor

  type(a) elemental function get_ao(this)
    class(c_sys_type), intent(in) :: this

    get_ao = this % ao
  end function get_ao

  subroutine set_ao(this, ao)
    class(c_sys_type), intent(inout) :: this
    type(a),           intent(in)    :: ao

    this % ao     = ao
    this % ao_set = .true.
  end subroutine set_ao

end module c_sys
  • 我假设您的
    A类
    AO类
    是在您未提供的
    CLASS_A
    模块中定义的。我在我的版本中声明了一个伪类型
如果你想使用
NOPASS
等,事情可能会变得更复杂,但对于“正常”用法,我希望这能回答你的问题