ユーザー定義型 Edit

概要 Edit

項目podstructclassinterface
デフォルトコンストラクタのユーザー定義xxox
コンストラクタooox
デストラクタxoox
静的コンストラクタ
静的デストラクタ
ooox
structメンバ変数xoox
objectメンバ変数xoox
メンバ関数oooo
メンバ変数ooox
ガベージコレクタ対象xxoo
インターフェースの継承xxoo
公開修飾子(public/private)ooox
代入オーバーロードxoxx
invariantooox

1 : 全部0で初期化

interface Edit

  • C#,Dのinterfaceと同じ
  • interfaceは複数にinterfaceを継承することができる
  • 純粋仮装関数にはabstractキーワードを先頭につける
  • <要検討>デフォルト実装を書くことを許す(本当は許さない方が厳密なinterfaceなんですけど)
すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
 
-
|
|
|
|
!
interface IButtonListener
{
    // 抽象関数(絶対実装)
    abstract void onButtonPush( in IButton sender )const;
    // デフォルト実装ありの関数
    void onButtonFocused( in IButton sender )const{}
};

class Edit

  • 強制的にobject型を継承している
  • 他の言語でよくあるclassの継承は不可
  • interfaceは何個でも継承できる

pod Edit

  • plain object data
  • C言語のstructに近い
  • デフォルトコンストラクタはオブジェクトのメモリ領域を0で初期化する扱いになる

struct Edit

  • 継承のないC++のクラス・D言語のscope classと同じ
  • ガベージコレクト対象ではないので参照を保持することができない

型公開修飾子 - public private(class,interface,pod,struct) Edit

  • デフォルトはpublic
  • 名前空間直下のprivateな型は同じソース内からしかアクセスができない
すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 
 
-
!
 
 
 
-
|
!
 
// 同じソース内からしかアクセスできない
private pod A
{
};
 
// 誰でもアクセスできる
pod B // public pod Bと同じ
{
  A a; // 同じソースなのでOK
};

メンバ公開修飾子 - public private(class,struct) Edit

  • public/privateのみ。protectedは無い。
  • デフォルトはpublic
  • コロンを使うC++スタイル、メンバ変数・関数に直接記述するJAVAスタイルの両方に対応
すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 
-
|
|
|
|
|
|
|
|
|
|
|
!
 
class A
{
    //--- 公開修飾子(デフォルトpublic)
    // C++/Dスタイル
public: 
    void funcPubA(){} // public
private:
    void funcPriA(){} // private
 
    // JAVA/C#/Dスタイル
    public void funcPubB(){} // public
    private void funcPriB(){} // private
 
};

オーバーライドチェック - override(class) Edit

  • D,C#と同じ

不変条件 - invariant(class,pod,struct) Edit

  • (all)Dのinvariantと同じ
  • <要検討>structで再帰呼び出しされたらどうしよう(classはカウンタ持たせればいいかな)

委譲 - implement(class) Edit

  • interfaceのデフォルト実装を別オブジェクトに委譲するキーワード
すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 
-
|
|
!
 
-
|
|
!
 
 
-
|
|
|
|
|
|
!
 
 
-
|
|
|
|
!
 
interface IButtonListener
{
    abstract void onPushed();
    void onFocused(){}
};
class Hoge : IButtonListener
{
public:
    override void onPushed(){}
};
 
class A : IButtonListener implement mHoge
{
    // IButtonListenerの関数を全てmHogeに委譲される
    // implementを使うと自動で次のように展開される
    // override void onPushed(){ mHoge.onPushed(); }
    // override void onFocused(){ mHoge.onFocused(); }
private:
    Hoge mHoge = new Hoge();
};
 
class B : public IButtonListener implement mHoge
{
    // onFocusedの関数だけ実装、その他はmHogeにまかせる
    override void onFocused() { /* */ }
private:
    Hoge mHoge = new Hoge();
};

コンストラクタ - this() (class,pod,struct) Edit

  • (all)D言語の形式を採用し、関数名はthisを使う
  • (all)別コンストラクタ関数コールは可能
  • (all)どのコンストラクタよりも前に.Initデータをメモリ領域にセットされる(D言語と同じふるまいをする)
  • (pod)デフォルトコンストラクタは定義できない(0初期化固定)
    すべてを展開すべてを収束
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
    
     
    -
    |
    |
    -
    |
    !
    |
    -
    |
    |
    |
    !
    |
    |
    |
    !
    
    class A
    {
    public:
        this()
        {
          mY = mX + 1;
        } 
        this(in int x, in int y)
        {
          this();
          mX = x;
          mY = y;
        }
     
        int x = 1; // 直接初期化子がかける(pod,struct限定)
        int y; // 指定がないとデフォルトコンストラクタが呼ばれる
    };

デストラクタ - ~this() (class,struct) Edit

  • D言語スタイルを採用
  0
  1
  2
  3
  4
  5
class A
{
public:
    ~this()
    {
    }
};

静的コンストラクタ・デストラクタ - static this(), static ~this() (class,struct,pod) Edit

  • D言語と同じ

メンバ関数/変数 (class,interface,struct,pod) Edit

  • (all)C++でいうメンバ関数のconst修飾子はあり。引数リストの括弧の後ろにconstを付ける。
  • (all)staticメンバ関数が使える。
  • (class,pod,struct)staticメンバ変数が使える。仕様はD言語と同じ。
  • (class,struct)公開修飾子をつけられる
  • (class)抽象関数にoverrideキーワードが使える
  • (interface)メンバ変数は使えない
  • (interface)純粋仮装関数には先頭にabstractキーワードを付ける
  • (interface)デフォルト実装はstatic関数として同じ名前の関数が定義される。
すべてを展開すべてを収束
  1
  2
  3
  4
  5
  6
  7
 
-
|
|
|
|
!
interface IName
{
  string getName(){ return "NoName"; }
  // こう書くと自動でstatic関数が作られる
  // static string getName() { return "NoName"; }
  // IName.getName()でアクセスできる
};

    ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS