Last active
October 10, 2018 13:55
-
-
Save bluepost59/be1975d070e3451c0aa51ab4d6d160f5 to your computer and use it in GitHub Desktop.
続・fortranでもオブジェクト指向したい!「継承と抽象クラス」 ref: https://qiita.com/Bluepost59/items/8d4b7d7713676fe06472
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type,extends(parent_class) :: child_class | |
integer param1 | |
!... | |
contains | |
procedure :: method1 => child_method1 | |
!... | |
end type child_class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type,abstract :: absclass | |
integer param2 | |
!... | |
contains | |
procedure(absmethod1),deferred :: method1 | |
procedure(absmethod2),deferred :: method2 | |
!... | |
end type absclass | |
!引用仕様(戻り値、引数)の定義 | |
interface | |
subroutine absmethod1(self,subparam) | |
import absclass | |
class(absclass) self | |
integer subparam | |
end subroutine absmethod1 | |
double precision function absmethod2(self,subparam) | |
import absclass | |
!... | |
end function absmethod2 | |
end interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program main | |
use mod_idol | |
implicit none | |
type(idol) :: myidol | |
character(len=60) :: myname = "Momoka" | |
character(len=60) :: myunit = "momo_pear_berry" | |
call myidol%set_name(myname) | |
call myidol%join(myunit) | |
write(6,*) "name:", myidol%get_name() | |
write(6,*) "unit:", myidol%get_group() | |
end program main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module mod_idol | |
implicit none | |
!--------------------------------------------------- | |
! 抽象クラス girl | |
type,abstract :: girl | |
private | |
character(len=60) :: name = "noname" | |
contains | |
procedure(girl_set_name),deferred :: set_name | |
procedure(girl_get_name),deferred :: get_name | |
end type girl | |
! 引用仕様の宣言 | |
interface | |
subroutine girl_set_name(self,str) | |
import girl | |
class(girl) self | |
character(len=60) str | |
end subroutine girl_set_name | |
character(len=60) function girl_get_name(self) | |
import girl | |
class(girl) self | |
end function girl_get_name | |
end interface | |
!--------------------------------------------------- | |
! クラス idolの宣言部 | |
type,extends(girl) :: idol | |
private | |
character(len=60) group | |
contains | |
procedure :: set_name => idol_set_name | |
procedure :: get_name => idol_get_name | |
procedure :: join => idol_join | |
procedure :: get_group => idol_get_group | |
end type idol | |
contains | |
!--------------------------------------------------- | |
! クラス idolの実装部 | |
subroutine idol_set_name(self,str) | |
class(idol) self | |
character(len=60) str | |
self%name = str | |
end subroutine idol_set_name | |
character(len=60) function idol_get_name(self) | |
class(idol) self | |
idol_get_name = trim(self%name) | |
end function idol_get_name | |
subroutine idol_join(self,group) | |
class(idol) self | |
character(len=60) group | |
self%group = group | |
end subroutine idol_join | |
character(len=60) function idol_get_group(self) | |
class(idol) self | |
idol_get_group = trim(self%group) | |
end function idol_get_group | |
end module mod_idol |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment