我想重写在C#中的方法,但我有不同的签名 [英] I would like to override a method in C#, but I have a different signature

查看:144
本文介绍了我想重写在C#中的方法,但我有不同的签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The base class user should access the original method

class A
 public init()

派生类用户应该aceess只有派生方法。

The derived class user should aceess ONLY the derived method.

class B
 public init(int info)

我不能使用覆盖卑诗省有不同的签名。
做什么选择我有这么派生类用户不会看到两种方法。

I cannot use "override" bc there's a different signature. What options do I have so that the derived class user does not see two methods.

注释。
所有的一切我只需要共享某些代码两班。继承是不是必须的。
,而简单的B的用户是当务之急。

Notes. All in all I just need two classes that share some code. Inheritance is not a must. But simplicity for the user of B is a priority.

推荐答案

这是一个很大的代码味道(和违反一些OOP的基本原则),并以我所知,也无法用任何语言来完成。在OOP中, B 的一个实例是 A 的实例;这是多态性。所以,如果 A 有一个名为的公共方法的init 接受任何参数,那就这样做 B

This is a big code smell (and violates some basic OOP tenets) and, to the best of my knowledge, can not be done in any language. In OOP, an instance of B is an instance of A; this is polymorphism. So if A has a public method named init accepting no parameters, then so does B.

什么是你想为

编辑做到这一点:现在你已经添加的编辑指出,继承不是必须的,只是使用组成共享代码。给 B A 的私有实例,例如

Now that you've added the edit that states that inheritance is not a must, just use composition to share code. Give B a private instance of A, for example.

这篇关于我想重写在C#中的方法,但我有不同的签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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