getters 和 setters 埃达

Getters and setters Ada

我刚开始在 Ada 上编程,我想知道如何编写 getter 和 setter 代码以使用 类 属性。

事实上,我有兴趣获取以下包的属性 deadlineperiodcomputingTime 的 getter 和 setter:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Real_Time; use Ada.Real_Time;

package body pkg_tasks is
    task body task_a is
        deadline : Time_Span := To_Time_Span(25.0);
        period : Time_Span := To_Time_Span(25.0);
        computingTime : Time_Span := To_Time_Span(10.0);
        startingTime : Time;
    begin
        entry start do
            startingTime := Clock;

            while (Clock - startingTime) < computingTime loop

            end loop;

            New_line;
            Put_Line("End of task A");
        end start;
    end task_a;
end pkg_tasks;

在任务的情况下,这很容易......你不能因为我们在 , tasks can only have entries which act as a way to synchronize tasks (read part on entries 和以下内容中回答的相同原因)。

但实际上,您可以执行一种 getter 作为条目,并根据您想要查询属性的时间使用选择性等待。

现在,关于设置任务的属性,在我看来,在开始条目上使用参数是最好的方法。

请注意,您写的是 classes 属性,但您目前根本没有使用 classes。任务是 Ada 中的第一个公民类型,并不像在 Java 中那样通过 class 类型实现。使用面向对象编程在这里是另一回事。

如上所述,通常任务不是正常的方式。回到 Ada83,没有受保护的类型,所以如果你需要类似的东西,那么你可以用一个任务来模拟它。除此之外,这里有一些使用任务、受保护类型和 类(或 Ada 称它们为标记类型)的示例:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Real_Time; use Ada.Real_Time;

procedure jdoodle is
    ------------------------------------------------------
    -- Task Example
    ------------------------------------------------------
    task Task_Example is
        entry Get(Time : out Time_Span);
        entry Set(Time : in  Time_Span);
    end Task_Example;

    task body Task_Example is
        Value : Time_Span := To_Time_Span(0.0);
    begin
        loop
            select
                accept Get(Time : out Time_Span) do
                    Time := Value;
                end Get;
            or
                accept Set(Time : in  Time_Span) do
                    Value := Time;
                end Set;
            or
                terminate;
            end select;
        end loop;
    end Task_Example;

    ------------------------------------------------------
    -- Protected type example
    ------------------------------------------------------
    protected type Protected_Example is
        procedure Put(Time : Time_Span); -- or use entry
        function Get return Time_Span;   -- or use procedure or entry
    private
        Value : Time_Span := To_Time_Span(0.0);
    end Protected_Example;

    protected body Protected_Example is
        procedure Put(Time : Time_Span) is
        begin
            Value := Time;
        end Put;
        function Get return Time_Span is
        begin
            return Value;
        end Get;
    end Protected_Example;

    ------------------------------------------------------
    -- Class Example
    ------------------------------------------------------
    package Classes is
        type Class_Example is tagged limited private;
        procedure Put(Self : in out Class_Example; Time : Time_Span);
        function Get(Self : in Class_Example) return Time_Span; -- or use procedure
    private
        type Class_Example is tagged limited record
            Value : Time_Span := To_Time_Span(0.0);
        end record;
    end Classes;

    package body Classes is
        procedure Put(Self : in out Class_Example; Time : Time_Span) is
        begin
            Self.Value := Time;
        end Put;

        function Get(Self : in Class_Example) return Time_Span is
        begin
            return Self.Value;
        end Get;
    end Classes;
begin
    Put_Line("Starting");
end jdoodle;

请记住标记类型示例也适用于常规记录和其他私有类型。