TypechoJoeTheme

MetMan's Blog

网站页面
搜索到 4 篇与 的结果 ———
2024-08-10

C面向对象

C面向对象
五星上将麦克阿瑟曾说过:“不是C不能OOP,而是C++更有性价比”。面向对象编程是一种编程思想,不和具体的语言工具绑定,但有些语言在语法层面支持使得面向对象编程实现更容易,比如C++,Python。我们常说C语言是一门面向过程的语言,但实际上也可以通过C结构体、函数指针来模拟实现面向对象编程。本文实例来自《嵌入式C语言自我修养》(王利涛编著)。类的模拟C语言中没有class关键字,但可以用结构体模拟一个类(C++中类定义也可以用struct),结构体成员类似C++类中属性,成员函数使用结构体中内嵌函数指针来模拟。// animal.c struct animal { int age; int weight; void (*fp)(void); //函数指针模拟成员函数 };类似于C++的类定义// animal.cpp class Animal { public: int age; int weight; void speak(void); //类方法 };如果需要模拟类的多个方法,一个结构体中需...
2024年08月10日
33 阅读
0 评论
2024-08-10

Fortran面向对象(三)

Fortran面向对象(三)
继续Fortran面向对象的学习。Unlimited polymorphic typesclass(*) 类型称为无限制多态类型,必须拥有pointer或allocatable属性。括号中用*代替具体的类名表示可以是任意类。(联想C语言中void *通用指针概念)一般不直接使用无限制多态类型,常用作过程的哑元,使用select type结构转换为指定类型使用。CLASS(*), POINTER :: unlimited_ptr unlimited_ptr => base_target SELECT type(unlimited_ptr) TYPE IS (base) PRINT *, "base type: component value: ", unlimited_ptr%i TYPE IS (child) PRINT *, "child type: component values: ", unlimited_ptr%i,unlimited_ptr%j END SELECTpass ...
2024年08月10日
36 阅读
0 评论
2024-07-31

Fortran面向对象 二

Fortran面向对象 二
面向对象编程核心概念包括封装、继承及多态。下面介绍Fortran的类继承及多态语法。Fortran类继承Fortran中类继承使用扩展(extends)关键字定义继承哪个父类。type mytype integer :: value end type mytype ! extend from mytype type, extends(mytype) :: mynewtype real :: extra end type mynewtype新的类mynewtype除了继承mytype中成员value,还定义了新成员extra。module mytypes implicit none type mytype integer :: value contains ! => 过程别名 procedure,public :: write => write_mytype end type mytype type, extends(mytype) :: mynewtype real :: extra end ty...
2024年07月31日
34 阅读
0 评论
2024-07-31

Fortran面向对象

Fortran面向对象
从Fortran 2003开始标准加强了面向对象(OOP)语法支持,通过使用模块(module)及复用派生类型关键字(type)定义Fortran的类。Fortran主要参照C++ OOP模型进行设计,两者对比如下表所示。 C++Fortran成员在类中定义在'type'中定义方法在类中定义在'type'中声明interface;在module中实现构造函数default或explicit没有对象本身this第一个参数对象方法引用点操作符'.’%操作符Fortran类组织方式在模块文件定义类(module ...type ...contains ... end type ...contains ... end module)type...contains...end type定义类(包括数据成员及类方法声明)实际方法定义在模块contains部分实现类方法的第一个参数是对象本身module A type B ! data members integer :: c contains procedure :: d end type cont...
2024年07月31日
36 阅读
0 评论

互动读者

标签云

最新回复

暂无回复

登录
X
用户名
密码