Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:使派生对象有条件地共享相同的基础对象_C#_Oop_Multiple Inheritance - Fatal编程技术网

C#:使派生对象有条件地共享相同的基础对象

C#:使派生对象有条件地共享相同的基础对象,c#,oop,multiple-inheritance,C#,Oop,Multiple Inheritance,我有一个基类,它由多个派生类继承。我正在初始化构造函数中基类的一些属性。是否有任何方法可以使基类属性由派生对象共享,而不是为每个派生类对象创建相同的属性值。这非常重要,因为一些基类属性值是由服务生成的,共享这些值可以提高性能。 下面是我想说的一些简单的蓝图: public class ClassA { //i dont want to use static here as it will be shared for multiple codes protected string

我有一个基类,它由多个派生类继承。我正在初始化构造函数中基类的一些属性。是否有任何方法可以使基类属性由派生对象共享,而不是为每个派生类对象创建相同的属性值。这非常重要,因为一些基类属性值是由服务生成的,共享这些值可以提高性能。 下面是我想说的一些简单的蓝图:

public class ClassA
{
    //i dont want to use static here as it will be shared for multiple codes
    protected string country { get; set; }
    public ClassA(string code)
    {
        country = CallsomeService(code);
    }
}

public class ClassB : ClassA
{
    public ClassB(string code) : base(code)
    {
        //blah blah
    }

    public void DomeSomethingWithCountry()
    {
        Console.WriteLine($"doing this with {country} in classB");
    }
}

public class ClassC : ClassA
{
    public ClassC(string code) : base(code)
    {
        //blah blah
    }

    public void DomeSomethingWithCountry()
    {
        Console.WriteLine($"doing soemthing else with {country} in classC");
    }
}
现在制作如下物体

     public void test()
        {
            //call service for this
            var classb=new ClassB("1");
            //dont call service for this
            var classc=new ClassC("1");
classb.DomeSomethingWithCountry();
classc.DomeSomethingWithCountry();
            //call service for this as code is different
            var classb1=new ClassB("2");
        }
您可以静态存储调用的结果,而不是值本身

public class ClassA
{
    static Dictionary<string,string> codeToCountryLookup
         = new Dictionary<string,string>();
    protected string country { get; set; }
    public ClassA(string code)
    {
       if(!codeToCountryLookup.ContainsKey(code))
            codeToCountryLookup.Add(code,CallsomeService(code));
       country = codeToCountryLookup[code];
    }
}
公共类ClassA
{
静态字典代码ToCountryLookup
=新字典();
受保护字符串国家{get;set;}
公共ClassA(字符串代码)
{
如果(!codeToCountryLookup.ContainsKey(代码))
Add(代码,CallsomeService(代码));
country=codeToCountryLookup[code];
}
}

这无论如何都不是线程安全的,但应该给您一个开始的地方。

如果我对所有代码都将其设置为静态,它将始终返回相同的国家/地区。在构造函数中传递一个对象实例,该实例包含
值(缓存)@SWAGATAPRATEK这些基类属性将仅由派生类使用methods@Unnie我花了一段时间才明白。删除了我的评论。如果他在检查字典是否包含某个键之前锁定了一个对象呢?这对多线程有帮助,对吗?是的,只是正确处理多线程情况远远超出了这个问题的范围。这看起来是一个很好的开始选择。我已经更新了我的帖子。如果我在派生类方法中使用此country属性,该怎么办。那么我怎样才能在派生类中有效地引用这个正确的国家呢methods@Unnie我对你的基类中的
国家
字段没有任何更改。您可以像以前一样继续使用它。虽然这会起作用,但我想知道缓存的正确位置是否在服务级别,而不是类级别。