Fortran Coder

查看: 5788|回复: 1
打印 上一主题 下一主题

[面向对象] 能将其中的部分代码进行抽象吗

[复制链接]

4

帖子

3

主题

0

精华

入门

F 币
31 元
贡献
18 点
跳转到指定楼层
楼主
发表于 2016-11-26 22:07:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[Fortran] 纯文本查看 复制代码
01Module MInteger_Array
02 
03  implicit none
04  
05 
06  !整数数组抽象
07   !策略模板(不同的type对应同一个subroutine)
08   type, abstract :: int_ary
09    integer                        :: iNs = 0
10    integer          , allocatable :: iAry(:)
11     contains
12       procedure(strategy_procedure), deferred, pass :: SRead_int_ary
13   end type int_ary
14 
15   abstract interface
16      subroutine strategy_procedure(this, iunit)
17         import  :: int_ary
18         implicit none
19         class(int_ary) , intent(inout) :: this
20         integer        , intent(in)    :: iunit
21      end subroutine strategy_procedure
22   end interface
23 
24  !2位整数
25  type, extends(int_ary) :: int_ary_2P
26    character(len=2) , allocatable :: CAry(:)
27    contains
28      procedure      , pass        :: SRead_int_ary => SRead_int_ary_2p
29  end type
30 
31  !3位整数
32  type, extends(int_ary) :: int_ary_3P
33    character(len=3) , allocatable :: CAry(:)
34    contains
35      procedure      , pass        :: SRead_int_ary => SRead_int_ary_3p
36  end type
37 
38  contains
39 
40    Subroutine SRead_int_ary_2p(this, iunit)
41      implicit none
42      class(int_ary_2P), intent(inout) :: this
43      integer          , intent(in)    :: iunit
44      read(iunit,*) this%iNs
45      allocate(this%iAry(this%iNs))
46      read(iunit,*) this%iAry
47      allocate(this%CAry(this%iNs))
48      call SInt_to_2Char(this%iAry, this%CAry)
49      Return
50    End Subroutine
51 
52    Subroutine SRead_int_ary_3p(this, iunit)
53      implicit none
54      class(int_ary_3P), intent(inout) :: this
55      integer          , intent(in)    :: iunit
56      read(iunit,*) this%iNs
57      allocate(this%iAry(this%iNs))
58      read(iunit,*) this%iAry
59      allocate(this%CAry(this%iNs))
60      call SInt_to_3Char(this%iAry, this%CAry)
61      Return
62    End Subroutine
63 
64End Module





这段代码中的
read(iunit,*) this%iNs
allocate(this%iAry(this%iNs))
read(iunit,*) this%iAry
allocate(this%CAry(this%iNs))

每次都是重复的吗,能提炼出来,抽象成一个函数吗
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

742

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
726 元
贡献
371 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

沙发
发表于 2016-11-27 10:53:52 | 只看该作者
本帖最后由 楚香饭 于 2016-11-27 11:14 编辑

这一问题,根本就不需要抽象类和子类。

[Fortran] 纯文本查看 复制代码
01Module MInteger_Array
02  implicit none
03  private
04  type , public :: int_ary
05   integer , private              :: iNs   = 0
06   integer                        :: Lens = 2!default 2
07   integer , private, allocatable :: iAry(:)
08   character(len=:) ,private , allocatable :: CAry(:)
09  contains
10     procedure      , pass        :: SRead_int_ary
11     final                        :: SRead_Uninit
12  end type int_ary
13contains
14  Subroutine SRead_int_ary(this , iunit )
15    class(int_ary), intent(inout) :: this
16    integer       , intent(in)    :: iunit
17    read(iunit,*) this%iNs
18    allocate(this%iAry(this%iNs))
19    read(iunit,*) this%iAry
20    if ( this%Lens < 1 ) this%Lens = 2
21    allocate( character(len=this%Lens)::this%CAry(this%iNs))
22    !call SInt_to_2Char(this%iAry, this%CAry)
23  End Subroutine SRead_int_ary
24  Subroutine SRead_Uninit(this)
25    type(int_ary), intent(inout) :: this
26    deallocate(this%iAry,this%CAry)
27  End Subroutine SRead_Uninit
28End Module
29 
30Program main
31  use MInteger_Array
32  type(int_ary) :: m2p=int_ary(Lens=2) , m3p=int_ary(Lens=3)
33  Open(3,File="filename.txt")
34  call m2p%SRead_Int_Ary( 3 )
35  call m3p%SRead_Int_Ary( 3 )
36  Close(3)
37End Program main


您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2025-4-30 13:48

Powered by Discuz! X3.4

© 2013-2025 Comsenz Inc.

快速回复 返回顶部 返回列表