如何设计/编写GlobalConfig类? [英] How to design/write GlobalConfig class?

查看:251
本文介绍了如何设计/编写GlobalConfig类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。



我需要创建一个GlobalConfig类。但是我想从另一个课程中得到它。



这是一个例子:



Hi.

I need to create a GlobalConfig class. But I want to derive from it in another class.

Here''s an example:

public class BaseConfig {
 public string GlobalPath {get; set;}
}

public class ConfigA :  BaseConfig  {
 public string pathA {get; set;}
}

public class ConfigB :  ConfigA  {
 public string pathB {get; set;}
}





背后的想法是我不想多次编写代码,而且更重要的是

类ConfigA我想设置GlobalPath并可以在ConfigB中访问它。



换句话说,我希望class ConfigB 拥有一个在ConfigA类中设置的属性 GlobalPath



澄清我想在内存中只有一个Config对象。



当我将BaseConf.GlobalPath设置为''A''时,我想从ConfigB.GlobalPath访问它并获得''A''。





我总是将GlobalConfig设计为静态class,但静态类不能继承。

所以我试图实现Singleton Pattern,但是ConfigA找不到类BaseConfig的构造函数,因为它是私有的。



我将感激所有的帮助和建议。

谢谢。



The idea behind is that I don''t want to write the code multiple times and what''s more important
in class ConfigA I want to set GlobalPath and have access to it in ConfigB.

In other word I want class ConfigB to have a property GlobalPath which was set in class ConfigA.

To clarify I want to have only one object of Config in memory.

When I set BaseConf.GlobalPath to ''A'', I want to access it from ConfigB.GlobalPath and also get ''A''.


I always design GlobalConfig as a static class, but static classes can''t be inherited.
So I tried to implement Singleton Pattern, but ConfigA can''t find constructor of class BaseConfig because it''s private.

I''ll appreciate all help and suggestions.
Thank you.

推荐答案

将私有构造函数更改为protected。



祝你好运!
Change the private constructor to protected.

Good luck!


这篇关于如何设计/编写GlobalConfig类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆