Fortran Coder

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

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

[复制链接]

4

帖子

3

主题

0

精华

入门

F 币
31 元
贡献
18 点
跳转到指定楼层
楼主
发表于 2016-11-26 22:07:23 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
[Fortran] 纯文本查看 复制代码
Module MInteger_Array

  implicit none
 

  !整数数组抽象
   !策略模板(不同的type对应同一个subroutine)
   type, abstract :: int_ary
    integer                        :: iNs = 0
    integer          , allocatable :: iAry(:)
     contains
       procedure(strategy_procedure), deferred, pass :: SRead_int_ary
   end type int_ary

   abstract interface
      subroutine strategy_procedure(this, iunit)
         import  :: int_ary
         implicit none
         class(int_ary) , intent(inout) :: this
         integer        , intent(in)    :: iunit
      end subroutine strategy_procedure
   end interface

  !2位整数
  type, extends(int_ary) :: int_ary_2P
    character(len=2) , allocatable :: CAry(:)
    contains
      procedure      , pass        :: SRead_int_ary => SRead_int_ary_2p
  end type

  !3位整数
  type, extends(int_ary) :: int_ary_3P
    character(len=3) , allocatable :: CAry(:)
    contains
      procedure      , pass        :: SRead_int_ary => SRead_int_ary_3p
  end type

  contains

    Subroutine SRead_int_ary_2p(this, iunit) 
      implicit none
      class(int_ary_2P), intent(inout) :: this
      integer          , intent(in)    :: iunit
      read(iunit,*) this%iNs
      allocate(this%iAry(this%iNs))
      read(iunit,*) this%iAry
      allocate(this%CAry(this%iNs))
      call SInt_to_2Char(this%iAry, this%CAry)
      Return
    End Subroutine

    Subroutine SRead_int_ary_3p(this, iunit) 
      implicit none
      class(int_ary_3P), intent(inout) :: this
      integer          , intent(in)    :: iunit
      read(iunit,*) this%iNs
      allocate(this%iAry(this%iNs))
      read(iunit,*) this%iAry
      allocate(this%CAry(this%iNs))
      call SInt_to_3Char(this%iAry, this%CAry)
      Return
    End Subroutine

End Module





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

每次都是重复的吗,能提炼出来,抽象成一个函数吗
分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

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

GMT+8, 2024-5-3 13:15

Powered by Tencent X3.4

© 2013-2024 Tencent

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