Object 接球手和接球手

Object 接球手和接球手,object,get,set,ada,Object,Get,Set,Ada,我刚开始在Ada上编程,我想知道如何编写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(2

我刚开始在Ada上编程,我想知道如何编写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;

在任务的情况下,这很容易。。。由于与我们在中的回答相同的原因,您不能这样做,任务只能有作为同步任务读取和以下内容的方式的条目

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

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


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

对于任务来说,很容易。。。由于与我们在中的回答相同的原因,您不能这样做,任务只能有作为同步任务读取和以下内容的方式的条目

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

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


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

如上所述,一般来说,任务不是正常的方式。在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;

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

如上所述,通常情况下,任务不是执行此操作的正常方式。在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;

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

好的,谢谢您的时间。正如你所看到的,我在这里很忙,哈哈,谢谢你的时间。正如你所看到的,我在这里很无聊哈哈