欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fortran面向对象编程简介

程序员文章站 2022-03-09 10:01:12
...
Module complex_class
    implicit none
    type, public  :: complex_ob  !.. 声明一个面向对象的type与对应的procedure
        private
        real      :: re
        real      :: im
    contains
        procedure :: cons => construct_complex
    end type complex_ob
    
    private       :: construct_complex
contains
    subroutine construct_complex( this, x, y, cm )  !.. 注意此处的this
        implicit none
        class( complex_ob )    :: this  !.. this的声明
        real, intent(in)       :: x, y
        complex, intent(inout) :: cm
        
        cm = cmplx(x, y)
    end subroutine construct_complex
    
End module complex_class    
    
Program testOOP
    use complex_class
    implicit none
    type(complex_ob) :: cm1  !.. 面向对象的声明
    real             :: x, y
    complex          :: cm
    
    cm = 0.
    x = 1.; y = 2.;
    call cm1%cons( x, y, cm ) !.. 调用type中的procedure
    print*, cm
    
End program testOOP
初次尝试,如有错误,敬请指正!

 

相关标签: fortran