문제

다음 코드가 주어지면 :

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, 그러나 인터페이스는 정적 멤버를 인터페이스의 일부로 정의 할 수 없습니다.

도움이 되었습니까?

해결책

각 파생 된 FOO 클래스의 FOO는 캐시를 얻는 방법/위치를 정의해야하므로 각각 (잠재적으로) 자체 캐시를 가질 수 있습니다. 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