Inheritance 如何使用接口块进行类型重载
我已经写了一个Fortran代码来读取不同的文本文件。每个文本文件都有自己的类型,该类型定义从定义常规操作的抽象类型继承的读取过程:Inheritance 如何使用接口块进行类型重载,inheritance,interface,fortran,overloading,fortran2003,Inheritance,Interface,Fortran,Overloading,Fortran2003,我已经写了一个Fortran代码来读取不同的文本文件。每个文本文件都有自己的类型,该类型定义从定义常规操作的抽象类型继承的读取过程: module FileImporter_class implicit none private type, abstract, public :: FileImporter . . contains procedure, public :: ProcessFile . . end type FileImporter contains . . subrout
module FileImporter_class
implicit none
private
type, abstract, public :: FileImporter
.
.
contains
procedure, public :: ProcessFile
.
.
end type FileImporter
contains
.
.
subroutine ProcessFile(self,FileName)
implicit none
! Declaring Part
class(FileImporter) :: self
character(len=*) :: FileName
! Executing Part
call self%SetFileName(FileName)
call self%LoadFileInMemory
call self%ParseFile
end subroutine ProcessFile
end module FileImporter_class
下面是继承类:
module optParser_class
use FileImporter_class
implicit none
type, public, extends(FileImporter) :: optParser
.
.
contains
procedure, public :: ParseFile
end type optParser
interface optParser
procedure ProcessFile
end interface
contains
.
.
end module optParser_class
我的问题是关于接口块的。我想通过简单地调用类型来调用过程
ProcessFile
,因此调用optParser('inputfile.txt')
。所示的这个变量给出了一个编译错误(ProcessFile不是函数也不是子例程)。我可以通过在optParser_类
模块中放置一个ProcessFile
函数来解决这个问题,但是我必须对每个继承类都这样做,这是我自然希望避免的。有什么建议吗?Fortran标准不允许将子例程放入重载类型名的接口块中
只有函数可以放入此类接口中,然后它们通常用于返回该类型的对象(构造函数或初始值设定项)
相反,您应该将其称为类型绑定过程,因为optParser
从FileImporter
继承它
call variable_of_optParser_type%ProcessFile('inputfile.txt')
在Fortran中,没有类似Python的类方法可以在没有实例的情况下调用。注意ProcessFile
有self
参数,因此它必须接收一些对象实例
顺便说一句,我建议你制定一个惯例,无论你的字体是以小写字母还是大写字母开头,都要坚持,以免混淆。非常感谢。我对Fortran中的OOP还是新手,所以我不知道什么是适用的和/或允许的,什么是不适用的。