现代Fortran:从后代调用祖先过程

编程入门 行业动态 更新时间:2024-10-24 22:22:36
本文介绍了现代Fortran:从后代调用祖先过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我开始使用Modern Fortran的OO功能,并且已经熟悉其他语言的OO.在Delphi(对象Pascal)中,通常在其重写后代过程中调用过程的祖先版本,甚至有继承"的语言声明也允许这种情况.我找不到等效的Fortran构造-但可能正在寻找错误的东西.请参阅下面的简单示例.任何建议,不胜感激.

I am starting to use the OO features of Modern Fortran and am already familiar with OO in other languages. In Delphi (Object Pascal) it is common to call an ancestor version of a procedure in its overridden descendent procedure and there is even a language statement "inherited" that allows this. I can't find an equivalent Fortran construct - but am probably looking for the wrong thing. See simple example below. Any advice much appreciated.

type tClass integer :: i contains procedure Clear => Clear_Class end type tClass type tSubClass integer :: j contains procedure Clear => Clear_SubClass end type tSubClass subroutine Clear_Class i = 0 end subroutine subroutine Clear_SubClass inherited Clear ! this is the Delphi way j = 0 end subroutine

推荐答案

以下是一些示例代码,试图通过@HighPerformanceMark实现注释(即,子类型具有引用父类型的隐藏组件). /p>

Here is some sample code that tries to implement the comment by @HighPerformanceMark (i.e., a child type has a hidden component that refers to the parent type).

module testmod implicit none type tClass integer :: i = 123 contains procedure :: Clear => Clear_Class endtype type, extends(tClass) :: tSubClass integer :: j = 456 contains procedure :: Clear => Clear_SubClass endtype contains subroutine Clear_Class( this ) class(tClass) :: this this % i = 0 end subroutine Clear_SubClass( this ) class(tSubClass) :: this this % j = 0 call this % tClass % Clear() !! (*) calling a method of the parent type end end program main use testmod implicit none type(tClass) :: foo type(tSubClass) :: subfoo print *, "foo (before) = ", foo call foo % Clear() print *, "foo (after) = ", foo print *, "subfoo (before) = ", subfoo call subfoo % Clear() print *, "subfoo (after) = ", subfoo end

提供(使用gfortran-8.2)

which gives (with gfortran-8.2)

foo (before) = 123 foo (after) = 0 subfoo (before) = 123 456 subfoo (after) = 0 0

如果我们注释掉以(*)标记的行,则subfoo % i保持不变:

If we comment out the line marked by (*), subfoo % i is kept unmodified:

foo (before) = 123 foo (after) = 0 subfoo (before) = 123 456 subfoo (after) = 123 0

更多推荐

现代Fortran:从后代调用祖先过程

本文发布于:2023-10-28 00:55:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535021.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:祖先   后代   过程   Fortran

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!