我想了解繼承了Fortran中是如何工作的,但有下面的代碼困難(對不起,如果這是一個常見問題...):重寫父程序具有相同名稱
module mymod
implicit none
type Pos
integer :: n_ = -1
contains
procedure :: set => Pos_set
endtype
type, extends(Pos) :: Vec
integer :: x_ = -2
contains
procedure :: set => Vec_set !! (1)
!! procedure :: set => Vec_set_p !! (2)
endtype
contains
subroutine Pos_set(p, n)
class(Pos) p
integer n
p% n_ = n
endsubroutine
subroutine Vec_set(v, n)
class(Vec) v
integer n
v% n_ = n
v% x_ = n * 10
endsubroutine
subroutine Vec_set_p(p, n)
class(Vec) p
integer n
p% n_ = n
p% x_ = n * 10
endsubroutine
end
program main
use mymod
implicit none
type(Pos) p
type(Vec) v
call p% set(10)
call v% set(20)
print *, "p = ", p
print *, "v = ", v
end
通過以上代碼,gfortran 5.2.1提供了以下錯誤消息:
procedure :: set => Vec_set
1
Error: Dummy argument ‘v’ of ‘set’ at (1) should be named ‘p’
as to match the corresponding argument of the overridden procedure
如果更改的set
到(即,由2替換第1行)的結合,其僅通過第一虛設的名稱是從Vec_set
不同爭論,然後程序編譯得很好,並給出了預期的結果。我也收到了ifort-14.0和Oracle Fortran的類似錯誤。
這是否意味着要重寫父類型的過程,我們還需要照顧虛擬參數的名稱嗎? (即,不僅是關於哪個類別的信息,還有變量的名稱?)
Like [this](http://stackoverflow.com/q/29011920)? – francescalus
這是否意味着,因爲Fortran允許類型構造函數與關鍵字參數一起使用(使用任意順序),我們需要保留與大多數父類中使用的名稱相同的名稱?因爲這在其他語言中可能並不常見(我認爲它們通常被認爲是位置參數),所以我覺得它很混亂(如果是這種情況......) – roygvib
是的,對於使用抽象的東西,我的問題看起來像是一個鏈接的重複。非常感謝您指向頁面:) – roygvib