美文网首页
Fortran Shape类结构(面向对象写法)

Fortran Shape类结构(面向对象写法)

作者: zoziha | 来源:发表于2020-10-20 23:32 被阅读0次

左志华(在读硕士)
哈尔滨工程大学 船舶工程学院
2020/10/20 星期二 晚 哈尔滨工程大学青岛科技园
ThinkPad E485 Windows 10 Home Edition;VS 2019;Intel Fortran 2020
欢迎留言 1325686572@qq.com

父类:Shape

module shape_class
    implicit none
    !> Abtract type
    type, public :: shape
        !> None
    contains
    procedure, public :: area => calc_area_fn
    procedure, public :: perimeter => calc_perimeter_fn
    procedure, public :: to_string => to_string_fn
    end type
    
    private :: calc_area_fn, calc_perimeter_fn, to_string_fn
    contains
    real function calc_area_fn(this)
    implicit none
    class(shape) :: this
    calc_area_fn = 0.0
    end function
    
    real function calc_perimeter_fn(this)
    implicit none
    class(shape) :: this
    calc_perimeter_fn = 0.0
    end function
    
    character(50) function to_string_fn(this)
    implicit none
    class(shape) :: this
    to_string_fn = ""
    end function
    
    end module

子类:Circle

module circle_class
    use shape_class
    implicit none
    type, public, extends(shape) :: circle
        real :: r = 0 !> Radius
    contains
    procedure, public :: inintialize => inintialize_sub
    procedure, public :: area => get_area_fn
    procedure, public :: perimeter => get_perimeter_fn
    procedure, public :: to_string => to_string_fn
    end type
    
    real, parameter :: PI = 3.1415926
    private :: inintialize_sub, get_area_fn, get_perimeter_fn
    contains
    subroutine inintialize_sub(this, r)
    implicit none
    class(circle) :: this
    real, intent(in) :: r
    this % r = r
    end subroutine
    
    real function get_area_fn(this)
    implicit none
    class(circle) :: this
    get_area_fn = PI * this % r**2
    end function
    
    real function get_perimeter_fn(this)
    implicit none
    class(circle) :: this
    get_perimeter_fn = 2.0 * PI * this % r
    end function
    
    character(50) function to_string_fn(this)
    implicit none
    class(circle) :: this
    write(to_string_fn, "(A,F6.2)") "Circle of radius", this % r
    end function
    
    end module

主程序:Console

program console
    use circle_class !> Import circle class
    implicit none
    type(circle), pointer :: cir !> Circle object
    integer :: i
    character(50) :: id_string
    integer :: istat
    type :: shape_ptr
        class(shape), pointer :: p
    end type
    
    type(shape_ptr),dimension(1) :: shapes
    allocate(cir, stat = istat)
    call cir % inintialize(2.0)
    !> Create the array of shape pointers
    shapes(1) % p => cir
    do i = 1, 1
        id_string = shapes(i) % p % to_string()
        write(6, "(/A)") id_string
        write(6, "(A, F8.4)") "Area = ", shapes(i) % p % area()
        write(6, "(A, F8.4)") "Perimeter = ", shapes(i) % p % perimeter()
    end do
    read(5, *)
    end program
    

运行结果


done

参考书目
[1] Stephen J. Chapman. Fortran 95/2003 程序设计. 第三版. 中国电力出版社. P671.

相关文章

  • Fortran Shape类结构(面向对象写法)

    左志华(在读硕士)哈尔滨工程大学 船舶工程学院2020/10/20 星期二 晚 哈尔滨工程大学青岛科技园Think...

  • JavaScript 面向对象编程

    写法 ES6面向对象的写法---类继承

  • Python基础-day11

    list ​ 面向对象 ​ 面向过程 ​ 项目流程 ​ 类的概念与写法 ​ 类的初始化 ​ ...

  • iOS(swift)类和结构体的区别

    类是面向对象编程;结构体是面向协议编程(面向对象的升级)。swift推荐在app中使用结构体(struct),类(...

  • 关于class

    关于class ES6 通过class 关键字,可以定义类。新的class写法让对象原型写法更加清晰,更像面向对象...

  • swift面相对象的基础<一>

    面向对象的核心是类和对象,面向对象的三大特征:封装,继承和多肽。swift可以定义枚举,结构体和类三种面向对象的实...

  • Swift--Swift语言中的面向对象特性

    Swift语言中的面向对象类型 枚举 结构体 类 可选链 访问限定 Swift语言中的面向对象类型 面向对象概念的...

  • JS第四天

    一、面向对象JS JS面向对象初始 1、属性与方法 使用属性解决循环绑定变量污染 2、类字典结构使用 结构 拓展 ...

  • 7.面向对象编程

    Swift 面向对象编程 面向对象的三大特性 继承封装多态 基本单元 枚举结构体类协议扩展 面向对象概述 从整体的...

  • class用法学习

    class面向对象定义 错误写法: class people: #定义类为人 def _init_(sel...

网友评论

      本文标题:Fortran Shape类结构(面向对象写法)

      本文链接:https://www.haomeiwen.com/subject/ehgjmktx.html