Fortran Coder

查看: 7933|回复: 5
打印 上一主题 下一主题

[面向对象] 隐藏派生类型的数据成员和自定义算符矛盾了怎么办?

[复制链接]

712

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
607 元
贡献
311 点

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

楼主
发表于 2018-11-5 23:12:38 | 显示全部楼层
[Fortran] 纯文本查看 复制代码
Module VectClass
  TYPE :: Vect
    Real(8) ,Private :: x
    Real(8) ,Private :: y
    Real(8) ,Private :: z 
  Contains
    Procedure :: set
    Procedure,pass(ThisVect),Private :: VectMulScal_Fn , ScalMulVect_Fn , VectMulVect_Fn
    Generic, Public :: operator (*) => VectMulScal_Fn , ScalMulVect_Fn , VectMulVect_Fn
    Procedure :: writef
    Generic :: write(formatted) => writef
  End TYPE

contains

  Subroutine set(this,x,y,z)
    class(Vect) :: this
    Real(8), Intent(IN) :: x , y , z
    this%x = x ; this%y = y ; this%z = z
  End Subroutine set

  Subroutine writef(this, unit, iotype, v_list, iostat, iomsg)
    class(vect), intent(in) :: this
    integer, intent(in) :: unit
    character (len=*), intent(in) :: iotype
    integer, intent(in) :: v_list(:)
    integer, intent(out) :: iostat
    character (len=*), intent(inout) :: iomsg
    write(unit , iotype(3:) , iostat=iostat, iomsg=iomsg) this%x , this%y , this%z
   End Subroutine writef

  Type(Vect) Function ScalMulVect_Fn(ThisVect,Scal) result( y )
    real(8) , Intent(IN) :: Scal
    class(Vect) , Intent(IN) :: ThisVect
    y = Vect( ThisVect%x * Scal , ThisVect%y * Scal , ThisVect%z * Scal )
  End Function ScalMulVect_Fn

  Type(Vect) Function VectMulScal_Fn(Scal,ThisVect) result( y )
    real(8) , Intent(IN) :: Scal
    class(Vect) , Intent(IN) :: ThisVect
    y = ThisVect * Scal
  End Function VectMulScal_Fn

  Type(Vect) Function VectMulVect_Fn(ovect,ThisVect) result( y )
    class(Vect) , Intent(IN) :: ovect , ThisVect
    y = Vect( ThisVect%x * ovect%x , ThisVect%y * ovect%y , ThisVect%z * ovect%z )
  End Function VectMulVect_Fn

End Module VectClass

Program Main
  use VectClass
  type(Vect) :: a , b , c
  call a%set(1.d0,2.d0,3.d0)
  b = a * 3.0d0
  write(*,'(a,dt"(3f6.2)")') "b=" , 3.0d0*b
  c = a * ( 3.d0 * b )
  write(*,'(dt"(3f6.2)")') c
End Program Main


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

本版积分规则

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

GMT+8, 2024-5-7 05:51

Powered by Tencent X3.4

© 2013-2024 Tencent

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