继承的延伸?
我有一个类:
class A {
method 1
method 2
method 3
}
现在我想添加一个新方法:方法4,但是我无法修改A类文件。 最佳实践是什么:
1. 我应该使用扩展吗?
2.我应该继承A并在那里实现新方法吗?如果是这样 - 子类的名称是什么?
后来我希望能够做到:
A ains=new A(); //A or other name
ains.method4();
I have a class:
class A {
method 1
method 2
method 3
}
now I would like to add a new method: method 4, nut I cannot modify class A file.
What is the best practice:
1. Should I use extensions?
2. Should I inherit A and there I'll implement the new method? If so - what will be the name of the child class?
Later on I would like to be able to do:
A ains=new A(); //A or other name
ains.method4();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这种情况下我通常遵循的规则是
如果我只是向现有类型添加行为而不是任何新数据,那么我会添加扩展方法。这是获取行为的侵入性最小的方式,并且消除了对深层对象层次结构的需要。
如果我需要添加一段数据以及该行为,那么我将考虑继承或包装在另一种类型中。
The rule I usually go by in this scenario is
If I'm just adding a behavior to an existing type but not any new data then I add an extension method. It's the least intrusive way of getting the behavior and removes the need for deep object hierarchies.
If there is a piece of data I need to add along with that behavior then I will consider inheritance or wrapping in yet another type.
取决于使用场景。
A
没有 Method4,因此接受A
作为参数的方法都不能依赖 Method4 的存在。如果您只需要 Method4 作为类内部的 Helper,请将其实现为扩展。否则,创建一个派生自
A
的类B
,以便需要Method4
的外部类可以可靠地请求B
作为他们的参数而不是A
。当然,例外总是适用的。 LINQ 是子类化不切实际的一个例子。
在评论中您提到了对财产的需求。在这种情况下,创建
class B : A
是您的主要选择,因为没有扩展属性。另一个选项是创建一个class B { public A TheA {get; set;} }
包围 A。但正如所说,这取决于现实世界的情况和用法。如果您绝对必须使用
A
并且需要调用此A
上的方法,那么您可以选择扩展方法,并且不能拥有属性。相反,创建 Get/Set 方法并将属性值存储在其他位置。Depends on the usage scenario.
A
does not have Method4, so no method that accepts anA
as it's parameter can rely on Method4 being existent.If you only need Method4 as a Helper internal to your class, implement it as an extension. Otherwise create a class
B
that derives fromA
so that external classes that needMethod4
can reliably request aB
as their parameter instead of anA
.Of course, Exceptions always apply. LINQ is an example of where subclassing would be impractical.
In a comment you mentioned the need for a property. In this case, creating a
class B : A
is your main option as there are no Extension Properties. The other option is creating aclass B { public A TheA {get; set;} }
which wraps around A.But as said, it depends on the real world case and usage. If you absolutely must work with an
A
and need to call methods on thisA
, then extension methods are your choice and you can't have properties. Instead, create Get/Set methods and store the property value somewhere else.听起来您需要使用扩展程序,但如果没有更多信息,很难判断。继承是指子级是
A
的类型,而不是只需要添加方法时。Sounds like you need to use an extension, but without more information it is hard to tell. Inheritance is when the child is a type of
A
, not when you just need to add a method.仅当您能够在任何想要使用派生类的引用的地方使用 A 的引用时,才使用继承。如果您的唯一目的是使用 A 中的方法,请使用委托。
另一方面,您可以尝试扩展。
Use inheritance only when you will be able to use the reference of A wherever you want to use the reference of the derived class. If your sole intention is to make use of the methods in A, use delegation.
On the other hand, you could try extensions though.