0

オブジェクト指向のパラダイムから考えると、タグ付けされたレコードのプライベート属性をどのように実装する傾向があるのでしょうか?

現時点では、唯一の方法は、プライベート タイプの属性を持つことです。

例えば

type car is tagged record
  i_am_a_public_attribute : Integer;
  i_am_another_public_attribute : Integer;
  my_private_attributes : t_private_attributes;
end record;

t_private_attributes は、パッケージのプライベート部分で宣言されています。

私が考えた2番目の方法は、たとえば継承を使用することでした

type car_public is tagged record
  i_am_a_public_attribute : Integer;
  i_am_another_public_attribute : Integer;
end record;

type car_private is new car_public with record
  my_private_attributes : Integer;
end record;

car_private はパッケージのプライベート部分で宣言されています。この実装は非常に面倒だと思いますが。

人々はどのようにこれを行う傾向がありますか?

ありがとうマット

4

2 に答える 2

5

Ada のパッケージの非公開部分は、非公開よりも C++ の保護に近いです。ただし、ほとんどの場合、プライベート パーツを使用するのが最善の方法であり、さらに、単体テストを子パッケージとして記述して、型の属性をテストできる柔軟性を提供します。

コードの他の部分から属性にアクセスできないようにしたい場合は、本体で属性を定義する必要があります。これは、パッケージのプライベート部分で宣言された不完全な型を使用して行うことができ、本体で完了します。次のように、型にはこの不完全な型へのポインターが含まれます。

package Foo is
    type My_Type is tagged private;
private
   type Private_Part;
   type Private_Part_Access is access Private_Part;
   type My_Type is tagged record
       P : Private_Part_Access;
   end record;
end Foo;
package body Foo is
   type Private_Part is record
      ...
   end record;
end Foo;

これは、Private_Part を抽象的なタグ付きの null レコードにし、本文で拡張することによっても実行できます。

もちろん、このスキームの難しさはメモリ管理です。これは、オブジェクトが対応するメモリを実際に解放することを確認する必要があるためです (おそらく制御された型を使用することによって)。

于 2014-09-17T12:05:42.420 に答える
2

これは、Brian Drummond が上記のコメントで書いたことの拡張です。私が Ada プログラミング言語で OOP を行い、いくつかのプライベート属性とパブリック属性を持つ「クラス」のアイデアを表現したい場合、次のように記述します (Matt の例を使用):

type Car_Type is tagged private;

function I_Am_A_Public_Attribute (This : Car_Type)       return Integer;
function I_Am_Another_Public_Attribute (This : Car_Type) return Integer;

private

type Car_Type is tagged
   record
      I_Am_A_Public_Attribute        : Integer;
      I_Am_Another_Public_Attribute  : Integer;
      I_Am_A_Private_Attribute       : Integer;
      I_Am_Another_Private_Attribute : Integer;
   end record;

function I_Am_A_Public_Attribute (This : Car_Type)       return Integer is (This.I_Am_A_Public_Attribute);
function I_Am_Another_Public_Attribute (This : Car_Type) return Integer is (This.I_Am_Another_Public_Attribute);

アイデアは、公開したい属性ごとに get 関数を持つことです。実際、上記のコードは、私が「Ada スタイル」と呼ぶものではありません。Ada の強みを活用するには、各属性に新しい型を定義します。

type I_Am_A_Public_Attribute_Type        is new Integer;
type I_Am_Another_Public_Attribute_Type  is new Integer;

type Car_Type is tagged private;

function I_Am_A_Public_Attribute (This : Car_Type)       return I_Am_A_Public_Attribute_Type;
function I_Am_Another_Public_Attribute (This : Car_Type) return I_Am_Another_Public_Attribute_Type;

private

type I_Am_A_Private_Attribute_Type       is new Integer;
type I_Am_Another_Private_Attribute_Type is new Integer;

type Car_Type is tagged
   record
      I_Am_A_Public_Attribute        : I_Am_A_Public_Attribute_Type;
      I_Am_Another_Public_Attribute  : I_Am_Another_Public_Attribute_Type;
      I_Am_A_Private_Attribute       : I_Am_A_Private_Attribute_Type;
      I_Am_Another_Private_Attribute : I_Am_Another_Private_Attribute_Type;
   end record;

function I_Am_A_Public_Attribute (This : Car_Type)       return I_Am_A_Public_Attribute_Type        is (This.I_Am_A_Public_Attribute);
function I_Am_Another_Public_Attribute (This : Car_Type) return I_Am_Another_Public_Attribute_Type  is (This.I_Am_Another_Public_Attribute);

get 関数と間違った属性を混在させると、コンパイル時にエラーが発生することに注意してください。これは、「エイダ、強力なタイピングを信頼する」の良い例です。

編集:パフォーマンスの観点から、パブリック属性と取得関数の選択に好みがあるかどうかを調査したことがあります。GNAT コンパイラを使用した場合、パフォーマンスに差がないことがわかりました。他のコンパイラで同じ実験を試みたことはありません。

于 2014-09-20T23:01:32.653 に答える