问题描述
我想知道如何从一个具有一个单类BaseClient继承并能够在继承类使用基本成员相同的实例从基类单身了。
I want to know how to inherit from a class 'BaseClient' that has a singleton and be able to use same instance of basic members from base class singleton in the inherited class too.
public class BaseClient
{
protected string _url;
protected string _username;
protected string _password;
private static BaseClient _instance;
private static readonly object padlock = new object();
public static BaseClient Instance
{
get
{
lock (padlock)
{
if (_instance == null)
{
_instance = new BaseClient(true);
}
return _instance;
}
}
}
public void SetInfo(string url, string username, string password)
{
_url = url;
_username = username;
_password = password;
}
public string GetVersion()
{
//MyService is a simple static service provider
return MyService.GetVersion(_url, _username, _password);
}
}
public class Advanced : BaseClient
{
private static AdvancedClient _instance;
private static readonly object padlock = new object();
public static AdvancedClient Instance
{
get
{
lock (padlock)
{
if (_instance == null)
{
_instance = new AdvancedClient(true);
}
return _instance;
}
}
}
public void DoAdvancedMethod()
{
MyService.DoSomething(_url, _username, _password);
}
}
所以,如果我用BaseClient.Instance.SetInfo(的http:// myUrl,MYUSER,MYPASSWORD);然后AdvancedClient.Instance.DoAdvancedMethod()时,AdvancedClient单将使用相同的基本成员实例作为BaseClient单身?
So if I use BaseClient.Instance.SetInfo("http://myUrl", "myUser", "myPassword"); and then AdvancedClient.Instance.DoAdvancedMethod(), the AdvancedClient singleton will use the same base member instance as the BaseClient singleton?
推荐答案
只是开个玩笑:)这里是我的解决方案:
Just kidding :) here's my solution:
我简单地使用一个独立的类来存储共享的成员,并在AdvancedClient单创建,检索BaseClient有一个
I simply use an independent class to store the shared members and on AdvancedClient singleton creation, retrieve the BaseClient one.
public class ClientInfo
{
public string Url { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
public class BaseClient
{
protected ClientInfo _info;
private static BaseClient _instance;
private static readonly object padlock = new object();
public static BaseClient Instance
{
get
{
lock (padlock)
{
if (_instance == null)
{
_instance = new BaseClient(true);
}
return _instance;
}
}
}
public ClientInfo Info
{
get
{
if(_info == null)
{
_info = new ClientInfo();
}
return _info;
}
}
public void SetInfo(string url, string username, string password)
{
Info.Url = url;
Info.Username = username;
Info.Password = password;
}
public string GetVersion()
{
//MyService is a simple static service provider
return MyService.GetVersion(Info.Url, Info.Username, Info.Password);
}
}
public class Advanced : BaseClient
{
private static AdvancedClient _instance;
private static readonly object padlock = new object();
public static AdvancedClient Instance
{
get
{
lock (padlock)
{
if (_instance == null)
{
_instance = new AdvancedClient(true);
_instance._info = BaseClient.Instance.Info;
}
return _instance;
}
}
}
public void DoAdvancedMethod()
{
MyService.DoSomething(Info.Url, Info.Username, Info.Password);
}
}
这篇关于如何做单继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!