我是一个基类:
class SDKBaseClass
{
public SDKBaseClass();
public SDKBaseClass(EndpointAddress remoteAddress);
}
创建SDKBaseClass的对象:
string host = "192.168.0.1";
string port = "4321";
Uri uri = new Uri(string.Concat(new String[] {
"net.tcp://", host, ":", port
}));
SDKBaseClass sdkBaseClass = new SDKBaseClass(new EndpointAddress(uri, new AddressHeader[0]));
我想创建继承自SDKBaseClass的子类:
class SDKChildClass : SDKBaseClass
{
SDKChildClass(string host, string port);
}
并在SDKChildClass构造函数中创建Uri对象:
public SDKChildClass(string host, string port)
{
Uri uri = new Uri(string.Concat(new String[] {
"net.tcp://", host, ":", port
}));
//what's next?
}
我迷路了,我不知道SDKChildClass构造函数中的下一步应该是什么。它应该从一个基类的构造函数继承吗?
PS。 SDKBaseClass来自DLL库,因此我不了解它的构造函数实现。
答案 0 :(得分:8)
将URI构造逻辑移动到一个方法并按如下方式调用它:
public SDKChildClass(string host, string port) : base(ConstructEndpoint(host, port))
{
}
这称为构造函数链接。你可以链接到base()和this()
答案 1 :(得分:2)
看来你正在寻找类似的东西:
class SDKChildClass : SDKBaseClass
{
SDKChildClass(string host, string port)
:base(new Uri(host, port)) // base class constructor call
{
}
}
如果您有精心设计的逻辑,请将其放入适当的private static
方法
class SDKChildClass : SDKBaseClass
{
private static Uri createUri(host, port)
{
//TODO: put all the logic here
}
SDKChildClass(string host, string port)
:base(createUri(host, port)) // base class constructor call
{
}
}
答案 2 :(得分:2)
class SDKChildClass : SDKBaseClass
{
public SDKChildClass(string host, string port)
: base(new EndpointAddress(new Uri("net.tcp://" + host + ":" + port)))
}