سؤال

ونظرا لالبرمجية التالية:

using System.Collections.Generic;
static class Program {
    static void Main() {
        bar Bar = new bar();
        baz Baz = new baz();
        System.Console.WriteLine(
            "We have {0} bars, rejoice!", bar.Cache.Count);
    }
}

public abstract class foo {
    public static List<foo> Cache = new List<foo>(); 
}

public class bar : foo {
    public bar() { Cache.Add(this); }
}
public class baz : foo {
    public baz() { Cache.Add(this); }
}

وتحصل على (المتوقع إلى حد ما) إخراج "لدينا 2 الحانات، ونبتهج!".

وهذا أمر رائع، لدينا الآن ضعف عدد الأماكن أن يكون لدينا البيرة (على ما يبدو)، ولكن ما أريد حقا هو لكل فئة لديها انها مخبأ الخاصة. السبب في أنني لا أريد أن تنفيذ هذه مجرد مخبأ في فئة فرعية لأن لدي أيضا بعض الأساليب في صفي المجردة التي يجب أن تكون قادرة على العمل على مخبأ (أي تكرار على كل واحد منهم). هل هناك طريقة للقيام بذلك؟ لقد بحثت في استخدام واجهة لfoo، لكن واجهة لا يسمح ليتم تعريف أعضاء ثابت كجزء من واجهة.

هل كانت مفيدة؟

المحلول

وكل فئة مشتقة من فو يجب أن تعرف كيف / أين يمكن الحصول على مخبأ، بحيث يمكن لكل (يحتمل) لديها ذاكرة التخزين المؤقت الخاصة. أساليب في فو يمكن أن يشير إلى GetCache () دون تنفيذ كونها معروفة.

public abstract class foo
{
    public abstract ICache GetCache();

    public void DoSomethingToCache()
    {
        ICache cache = this.GetCache();
        cache.DoSomething();
    }
}

public class bar : foo
{
    public static ICache BarCache = new FooCache();

    public override ICache GetCache()
    {
        return bar.BarCache;
    }
}

public class FooCache : ICache { }

نصائح أخرى

استخدم قاعدة الطبقة العامة معلمات مع فئة فرعية:

using System.Collections;
using System.Collections.Generic;

static class Program
{
    static void Main()
    {
        bar Bar = new bar();
        baz Baz = new baz();
        System.Console.WriteLine(
                "We have {0} bars, rejoice!", Bar.GetCache().Count);
    }
}

public abstract class foo<T>
{
    private static List<foo<T> > Cache = new List<foo<T> >();

    public IList GetCache()
    {
        return Cache;
    }
}

public class bar : foo<bar>
{
    public bar() { GetCache().Add(this); }
}
public class baz : foo<baz>
{
    public baz() { GetCache().Add(this); }
}
public abstract class foo {
    public abstract List<foo> Cache { get; }

    protected static Dictionary<Type, List<foo>> InnerCache = new Dictionary<Type, List<foo>>(); 
}

public class bar : foo {
    public override List<foo> Cache { 
       get { return foo.InnerCache[typeof(bar)]; } 
    }

    public bar() { Cache.Add(this); }
}

public class baz : foo {
    public override List<foo> Cache { 
       get { return foo.InnerCache[typeof(baz)]; } 
    }

    public baz() { Cache.Add(this); }
}

إليك الجواب:

using System;
using System.Collections.Generic;
static class Program
{
    static void Main()
    {
        var bar = new Bar();
        var baz = new Baz();
        System.Console.WriteLine(
                "We have {0} bars, rejoice!", Bar.Cache.Count);

        bar.PrintList();
        baz.PrintList();
    }
}

public abstract class Foo<T>
{
    public static List<T> Cache = new List<T>();

    public void PrintList()
    {
        foreach(var item in Cache)
        {
            Console.WriteLine(item);

        }
    }
}

public class Bar : Foo<Bar>
{
    public Bar() { Cache.Add(this); }
}
public class Baz : Foo<Baz>
{
    public Baz() { Cache.Add(this); }
}

وحاول هذا:

using System.Collections.Generic;
using System;
static class Program
{
  static void Main()
  {
    Bar bar = new Bar();
    Baz baz = new Baz();
    System.Console.WriteLine(
            "We have {0} bars, rejoice!", bar.Cache.Count);
    System.Console.ReadKey();
  }
}

public abstract class Foo
{
  public Foo()
  {
    Cache = new List<string>();
  }
  public List<String> Cache { get; set; }
}

public class Bar : Foo
{
  public Bar() 
  { 
    Cache.Add("Bar"); 
  }
}
public class Baz : Foo
{
  public Baz() { Cache.Add("Baz"); }
}

وآسف اضطررت الى تغيير الغلاف .. وكان مما رأسي ينفجر

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top