Frage

Bei dem folgenden Code:

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); }
}

Sie erhalten den (etwas erwartet) Ausgang "Wir haben 2 Bars, freuen sich!".

Das ist fabelhaft, wir haben jetzt doppelt so viele Orte, unser Bier (scheinbar) haben, aber was ich wirklich will ist für jede Klasse einen eigenen Cache zu haben. Der Grund, warum ich will nicht nur diesen Cache in der Unterklasse implementieren ist, weil ich einige Methoden auch in meiner abstrakten Klasse, die auf den Cache zu arbeiten in der Lage sein muss (nämlich alle von ihnen durchlaufen). Gibt es eine Möglichkeit, dies zu tun? Ich habe bei über eine Schnittstelle für foo gesucht, aber die Schnittstelle erlaubt keine statischen Elemente als Teil der Schnittstelle definiert werden.

War es hilfreich?

Lösung

Jede abgeleitete Klasse von foo sollte definieren, wie / wo einen Cache zu bekommen, so dass jeder kann (potentiell) hat einen eigenen Cache. Methods in foo GetCache () ohne die Umsetzung ist bekannt beziehen kann.

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 { }

Andere Tipps

Verwenden Sie eine generische Basisklasse mit der Unterklasse parametriert:

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); }
}

Hier ist Ihre Antwort:

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); }
}

versuchen Sie dies:

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"); }
}

Sorry, ich hatte das Gehäuse zu ändern .. Es machte meinen Kopf explodieren

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top