能不能通过绑定的方法重载函数名?
之所以有这个需求,还是处于隐藏派生类型数据成员和限制模块进程引用的考虑还是以建立一个三维矢量的派生类型为例,如果只是重载算符的话,
经过版主的指点,我已经知道可以这样:
TYPE :: Vect
REAL(8),Private :: x
REAL(8),Private :: y
REAL(8),Private :: z
Contains
Procedure,Pass(This),Private :: VectAddVect_Fn
Generic,Public :: Operator(+) => VectAddVect_Fn
End TYPE Vect
Private :: VectAddVect_Fn
Contains
Function VectAddVect_Fn(This,Another)Result(Ans)
CLASS(Vect), Intent(in) :: This,Another
TYPE(Vect) :: ans
ans%x=This%x+Another%x
ans%y=This%y+Another%y
ans%z=This%z+Another%z
End Function
这样的话,模块外只有通过“+”号才能操作Vect对象的数据成员,其他所有方法都是不可用的
实现了最大程度的保护,数据不会被以任何不希望的方式修改
但是如果不是定义算符,而是希望重载函数,比如重载内置函数abs,使得实现的效果是每个分量都去绝对值
能不能用类似的办法实现呢?
目前有一些其他的思路,但都难免会留下一些不希望留下的漏洞
比如可以设定一个绑定的Set进程,设为类型的私有方法,并在模块内也设为私有,
这样实质上就只能在模块内直接用这个进程而不能到处都将它作为一个成员方法引用
然后通过它用一个例如AbsVect_Fn的函数实现想要的重载abs函数功能,
然后用interface实现abs重载AbsVect_Fn的函数,看起来实现了目的
可是这是不是留下了一个AbsVect_Fn函数能被其他地方引用,这不是希望的结果……
洁癖是一种心理疾病。
绑定的方法只能 v = v%abs()
不能是 v= abs(v) 把 AbsVect_Fn 设置为 private 就好了。
一般,OOP的模块,都默认 private,然后把需要的 public
也可以双绑定,即绑定类的过程,也 overloading intrinsic function
这样
write(*,'(2dt"(3f6.2)")') abs(a) , a%abs()
都可以使用
Module VectClass
Implicit None
private
TYPE , public :: 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
Procedure :: abs => AbsVect_Fn
End TYPE
public :: abs
interface abs
module procedure AbsVect_Fn
end interface
contains
function AbsVect_Fn (ThisVect)
type(Vect) :: AbsVect_Fn
class(Vect) :: ThisVect
AbsVect_Fn = Vect( abs(ThisVect%x) , abs(ThisVect%y) , abs(ThisVect%z) )
end function AbsVect_Fn
fcode 发表于 2018-11-7 11:30
把 AbsVect_Fn 设置为 private 就好了。
一般,OOP的模块,都默认 private,然后把需要的 public
版主这么一说,我就明白了,说到底还是我对模块的隐藏还是不够理解
实际上用Interface定义通用名的话,那些实际实现的专用名进程都可以隐藏
只要通用名是public的,这样就能且只能用通用名来实现对专用名进程的引用了
不过双绑定overloading intrinsic function是什么呢,这个不懂…… 双绑定是指
Procedure :: abs => AbsVect_Fn
加上
Interface abs
Module Procedure AbsVect_Fn
End Interface
这些么?
这个概念是怎么回事,不是很明白,不知道版主能不能指导下…… 是的,这就是双绑定。(双绑定是我的一个口语,不是正式说法)
1. type bound procedure :派生类型绑定过程(成员变量)
2. overloading intrinsic function:重载内部函数 fcode 发表于 2018-11-8 08:45
是的,这就是双绑定。(双绑定是我的一个口语,不是正式说法)
1. type bound procedure :派生类型绑定过 ...
好的,非常感谢版主! fcode 发表于 2018-11-8 08:45
是的,这就是双绑定。(双绑定是我的一个口语,不是正式说法)
1. type bound procedure :派生类型绑定过 ...
F2003中
procedure :: xx
Generic, Public :: operator (*) =>xx
是不是可以完全替代F95中的
public :: operator (*)
interface operator (*)
module procedure xx
end interface procedure :: xx
Generic, Public :: operator (*) =>xx
这种必须绑定派生类型。
页:
[1]