2016-10-27 12 views
1

我已經編寫了Fortran代碼來讀取不同的文本文件。每一個文本文件都有自己的類型,定義了從抽象類定義的一般操作繼承了閱讀過程:如何使用接口塊進行類型重載

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,所以call optParser('inputfile.txt')。顯示的這個變體給出了一個編譯錯誤(ProcessFile不是函數或子例程)。我可以通過在optParser_class模塊中添加一個ProcessFile函數來解決這個問題,但是我必須爲每個繼承類都做這件事,我很自然地想避免這種情況。任何建議如何做到這一點?

回答

1

Fortran標準不允許將子例程放入重載類型名稱的接口塊中。

只有函數可以放入這樣的接口中,然後它們通常用於返回該類型的對象(構造函數或初始化方法)。

相反,你應該只是把它作爲一種結合的過程中,因爲optParserFileImporter繼承它

call variable_of_optParser_type%ProcessFile('inputfile.txt') 

有沒有類Python classmethods它可以在不Fortran語言的實例調用。通知ProcessFileself參數,所以它已經到接收一些對象實例。

順便說一句,我建議你做一個約定,無論你的類型是以小寫還是大寫字母開頭並堅持以避免混淆。

+0

非常感謝。我還是Fortran的OOP新手,所以我不知道什麼是適用和/或允許的,什麼不是。 – THo

相關問題