Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有可能实施;“摘要”;Fortran 2003中类型内的变量?_Fortran_Prototype_Abstract Class - Fatal编程技术网

是否有可能实施;“摘要”;Fortran 2003中类型内的变量?

是否有可能实施;“摘要”;Fortran 2003中类型内的变量?,fortran,prototype,abstract-class,Fortran,Prototype,Abstract Class,我想写一个抽象类型 type, abstract :: Vehicle real, dimension(:), allocatable:: Wheels contains procedure (Compute_Weight), deferred :: VehicleWeight end type Vehicle 也就是说,我希望在数组的抽象类型中有一个占位符,这样它就可以在扩展类型中被重写或重新定义 type, extends(Vehicle) :: Bike

我想写一个抽象类型

type, abstract :: Vehicle
    real, dimension(:), allocatable:: Wheels 
    contains
     procedure (Compute_Weight), deferred :: VehicleWeight
end type Vehicle
也就是说,我希望在数组的抽象类型中有一个占位符,这样它就可以在扩展类型中被重写或重新定义

type, extends(Vehicle) :: Bike
     allocate(Wheels(2))
    contains
     procedure :: VehicleWeight => BikeWeight
end type Bike

    type, extends(Vehicle) :: Car
     allocate(Wheels(4))
    contains
     procedure :: VehicleWeight => CarWeight
end type Car
GCC编译器会抱怨(我想这是对的),我能找到的解决这个问题的唯一办法就是不在抽象类型中声明可分配函数,而是直接在类型中声明大小正确的变量。
尽管如此,我还是希望有一种占位符来强制实现由Wheels(原型)描述的基本属性。I

组件的分配是一个可执行的操作-它需要出现在源代码的可执行部分中。考虑一下:

type, abstract :: vehicle
  real, dimension(:), allocatable :: wheels
  ...
end type

type, extends(vehicle) :: bike
  ...
end type bike

type, extends(vehicle) :: car
  ...
end type car

interface bike
  procedure bike_constructor
end interface bike

interface car
  procedure car_constructor
end interface car

...

function bike_constructor()
  type(bike) :: bike_constructor
  allocate(bike_constructor%wheels(2))
  ...
end function bike_constructor

function car_constructor()
  type(car) :: car_constructor
  allocate(car_constructor%wheels(4))
  ...
end function car_constructor
在Fortran 2008中,这可以用以下简单的方式使用:

class(vehicle), allocatable :: obj
IF (i_feel_like_some_exercise) THEN
  obj = bike()
ELSE
  obj = car()
END IF
PRINT "('My object has ',I0,' wheels!')", SIZE(obj%wheels)
在Fortran 2003中,不支持对多态对象进行内部赋值。需要使用变通方法,例如在ALLOCATE语句中使用源说明符

对组件和过程适当地应用public和private可以进一步指导和约束客户机代码以正确的方式与类型交互