基类用户应该访问原始方法
class A
public init()
Run Code Online (Sandbox Code Playgroud)
派生类用户应仅使用派生方法.
class B
public init(int info)
Run Code Online (Sandbox Code Playgroud)
我不能使用"覆盖"bc有一个不同的签名.我有什么选项,以便派生类用户看不到两个方法.
笔记.总而言之,我只需要两个共享一些代码的类.继承不是必须的.但是B的用户的简单性是优先考虑的.
编译器错误关键字"this"在当前上下文中不可用
delegate void CallBack(int i);
class A
{
public A(CallBack cb) { }
}
class B : A
{
public B() : base(new CallBack(this.f)){}
private void f(int i) { }
}
Run Code Online (Sandbox Code Playgroud)
为什么会出错?作为一种解决方案,我想在A()中提供无参数保护的ctor并且具有
class B : A
{
public B() : base() // inherit the new A() ctor
{
base.cb = new CallBack(this.f); //this is allowed here
}
//...
}
Run Code Online (Sandbox Code Playgroud)