定义了一个deferred的过程之后,父类可以调用这个过程。这样形成统一的接口。
其他使用 class(父类) 为参数的过程,也可以调用这个过程,而不需要判断对象属于哪个子类。(因为每个子类都有一个相同接口的函数)
给你一个例子
[Fortran] 纯文本查看 复制代码 Module Abst Implicit None
Type , abstract , public :: T_People
character(len=30) :: name
contains
Procedure(itf_intro), deferred :: introduction
Procedure :: whoami
End Type T_People
interface
Subroutine itf_intro(this)
import
class(T_People):: this
End Subroutine itf_intro
end interface
contains
Subroutine whoami( this )
class(T_People) :: this
write(*,*) "Who am I ?"
call this%introduction()
End Subroutine whoami
End Module Abst
Module People
use Abst
Implicit None
Type , extends(T_People), public :: T_Chinese
contains
Procedure :: introduction => intro_chinese
End Type T_Chinese
Type , extends(T_People), public :: T_English
contains
Procedure :: introduction => intro_english
End Type T_English
contains
Subroutine intro_chinese(this)
class(T_Chinese):: this
write(*,*) "我的名字叫:" , this%name
End Subroutine intro_chinese
Subroutine intro_english(this)
class(T_English):: this
write(*,*) "My name is:" , this%name
End Subroutine intro_english
End Module People
Program Main
use People
Implicit None
type(T_Chinese) :: xiaoming = T_Chinese("小明")
type(T_English) :: John = T_English("John")
call xiaoming%introduction()
call some(xiaoming)
call some(John)
contains
Subroutine some(someone)
class(T_People) :: someone
call someone%whoami()
End Subroutine some
End Program Main |