小编use*_*010的帖子

我想在C#中覆盖一个方法,但我有一个不同的签名

基类用户应该访问原始方法

class A
 public init()
Run Code Online (Sandbox Code Playgroud)

派生类用户应仅使用派生方法.

class B
 public init(int info)
Run Code Online (Sandbox Code Playgroud)

我不能使用"覆盖"bc有一个不同的签名.我有什么选项,以便派生类用户看不到两个方法.

笔记.总而言之,我只需要两个共享一些代码的类.继承不是必须的.但是B的用户的简单性是优先考虑的.

c#

3
推荐指数
2
解决办法
4058
查看次数

构造函数初始值设定项不允许我使用'this'

编译器错误关键字"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)

c# inheritance constructor

0
推荐指数
1
解决办法
854
查看次数

标签 统计

c# ×2

constructor ×1

inheritance ×1