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
等,事情可能会变得更复杂,但对于“正常”用法,我希望这能回答你的问题