4

次のように定義された2つのクラスがあります。

最初の1つ:

internal class Content {

   internal Content(Master master) {
        // code omitted
   }

// code omitted
}

二つ目:

public class Master {

     internal Content content { get; set; }

     internal Master() {
         // code omitted
     }

// code omitted
}

コンテンツ クラスをマスターのプロパティとして公開する 次のようなことを行う必要があります。

Master M = new Master(); 
M.content = new Content(M); 

コンテンツ コンストラクターでマスター (M) を渡さない方法はありますか?

4

3 に答える 3

4

おそらくContent 必要ですMasterか?実際、コンストラクターはこれを管理するためのかなり良い方法ですが、それが問題になる場合は、次のようにすることもできます。

internal class Content {
    internal void SetMaster(Master master) {this.master = master; }
    //...
}
internal class Master {
    internal void SetContent(Content content) {
        if(content == null) throw new ArgumentNullException("content");
        // maybe handle double-calls...
        this.content = content;
        content.SetMaster(this);
    }
}
...
Master M = new Master();
M.SetContent(new Content());

または、デフォルトで をMaster作成Contentします。率直に言って、「これは問題だ」という実際の事態が発生するまでは、「そのまま」にしておきます。

于 2012-07-23T08:14:49.670 に答える
3

遅延初期化idomを使用しないのはなぜですか?

public class Master
{
    private Content _content;

    internal Content content
    {
        get
        {
            if (_content == null)
            {
                _content = new Content(this);
            }
            return _content;
        }
    }
}

Master常にcontentプロパティを設定する必要がある場合は、Content構築中にメンバーを作成します。

public class Master
{
    internal Content content
    {
        get; private set;
    }

    internal Master()
    {
        content = new Content(this);
    }
}

混合アプローチを使用することもできます。

public class Master
{
    internal Content content
    {
        get; private set;
    }

    internal Content GetOrCreateContent()
    {
        if (content == null)
        {
            content = new Content(this);
        }
        return content;
    }

    internal Master()
    {
    }
}
于 2012-07-23T08:14:36.803 に答える
1

クラス内のコードは示されていないので、私はあなたが何をしようとしていたかをいくつか仮定する必要があります。あなたのコードからわかるように、ContentニーズMasterとは。Masterなしでは生きていけませんContent

私があなたのために作った解決策で、あなたは以下をすることができます:

void Main()
{
    Master M1 = new Master(); // content instantiated implicitly
    Master M2 = new Content().master; // master instantiated implicitly
}

したがって、MasterContentをインスタンス化するか、またはその逆を行うことができます。aをインスタンス化するContentと、Masterは暗黙的にインスタンス化されます。

選択した選択肢に関係なく、対応する他のオブジェクトは常にインスタンス化され、プロパティ変数を介して使用できます。

この例のクラスは次のように定義されています。

internal class Content  {  

    internal Master master { get; set; }

    internal Content(Master pmaster) {     
        master=pmaster;
    }

    internal Content() {     
        master = new Master() { content = this };
    }  
}

public class Master {  

    internal Content content { get; set; }  

    internal Master() {     
        content = new Content(this);
    }  

    // this is optional and can be omitted, if not needed:  
    internal Master(Content pcontent) {     
        content = pcontent;
    }  
} 

私はあなたがあなたに質問した構造を可能な限り忠実に保ちましたが、あなたは今や余分な柔軟性を持っていることに注意してください。

于 2012-07-23T08:47:35.510 に答える