2

私は、Ada の継承といくつかの構文について頭を悩ませています。

私の目標は、レコードを持つ抽象型から派生し、レコード フィールドで別のデータ型を使用することです。これが私がコンパイルできたものです:

type Base is abstract new Parent.ParentType with record
    X:Access_Type;
end record

type Child is new Base with record
    n:Integer;
end record;

しかし、この追加の n フィールドは必要ありません。X を子型の整数にしたいのです。コンパイラに満足してもらうことはできません。次のようなものが私が欲しいものです:

type Base is abstract new Parent.ParentType with tagged record
    X:Access_Type;
end record;

type Child is new Base with record
    X:Integer;
end record;

残念ながら、Xフィールドを再割り当てできると思われるベースタイプにタグを付ける方法がわかりません。(タグ付けしないと、コンパイラは競合する宣言について不平を言います。)

誰かがこれに光を当てることができますか? 私はオブジェクト指向プログラミング全般にまったく慣れていないので、Ada の型アプローチは通常のクラス アプローチよりもややこしいと感じています。

4

3 に答える 3

4

いくつかのレコードをネストしたいだけではありませんか?

   type Base is abstract new Parent.Parent_Type with record
      X : Float;
   end record;

...

type child_rec is 
  X : integer;
end record;

...

   type Child is new Bases.Base with record
      C : Child_Rec;
   end record;

これにより、参照できるようになります

My_Base.X;

My_Base.C.X;

もちろん、これはOO機能がなくても実行できます....

于 2012-10-14T11:28:43.647 に答える
4

I'm not sure what problem you are trying to solve by changing the type of X in a derived type. As suggested in Ada 95 Rationale: II.1 Programming by Extension, extension of a tagged type adds components to those inherited from the base type. You may be looking for a way to specify parameters for a derived type using discriminants.

Addendum: It may help to understand that Ada's support for common object-oriented programming principles is not limited to tagged types.

于 2012-10-13T08:58:02.570 に答える
3

Baseis abstract new Parent.Parent_Typeがタグ付けされていないと言うことができないため、Parent.Parent_Typeタグ付けする必要があります。つまり、 などの派生型もタグ付けするBase必要があります。

問題は、あなたが持っているように、見ることができるコードChildは2つを見ることができるということXです。の 1 つとBaseの 1 つChild。コンパイラーはあいまいになることを許しません。他の人があなたのコードを読んで を見たとき、彼らはあなたが何を意味しMy_Child.Xているかをどうやって知るのでしょうか?X

Baseこれを回避する 1 つの方法は、 private を完全に宣言することMy_Child.Xです。

package Bases is
   type Base is abstract new Parent.Parent_Type with private;
private
   type Base is abstract new Parent.Parent_Type with record
      X : Float;
   end record;
end Bases;

with Bases;
package Children is
   type Child is new Bases.Base with record
      X : Integer;
   end record;
end Children;
于 2012-10-13T08:56:27.323 に答える