公共的嵌套类型
我很好奇当涉及到 .NET 中的嵌套类型的某些场景时,什么是好的实践。
假设您有一个 Wheel 类,并且 Wheel 类包含 Bearing 对象。 Bearing 对象仅在 Wheel 内才有意义,并且您不希望允许独立创建它,因此将 Bearing 类嵌套在 Wheel 对象内是有意义的。但是,假设您现在需要读取 Wheel 类外部的 Wheel.Bearings 属性。现在需要将嵌套的 Bearing 类公开。
在这种情况下,最好的选择是什么?
1 - 创建嵌套在 Wheel 类中的公共 Bearing 类
2 - 创建一个独立的 Bearing 类,该类在其构造函数中采用 Wheel 对象
3 - 创建一个 Wheel 命名空间并在该命名空间内创建一个独立的 Bearing 类。
4 - 还有别的事吗?
更新: 我正在更新此内容以提供更多详细信息并反映迄今为止的一些建议。 ClassParent是父类,ClassChild是子类。 ClassChild 始终是 ClassParent 的子级,它单独存在是没有意义的。问题是 ClassChild 有一些属性需要公开公开,而其余所有属性只能从 ClassParent 调用。一个示例是 ClassChild.Delete 函数,该函数不应公开公开,因为它只能从 ClassParent 调用,因为 ClassParent 需要执行适当的清理和修改。
在查看了这些建议后,我提出的设计对我来说看起来有点不干净,所以我想我应该征求意见。我有:
public class Parent
{
ChildNested childObj
public DeleteChild()
{
//expose this method publically
childObj.DeleteChild()
//other functionality
}
public Child GetChild()
{
//expose Child, not ChildNested publically
return childObj
}
private class ChildNested:Child
{
public Child()
{
Base.Child()
}
public DeleteChild()
{
Base.Delete()
}
}
public abstract class Child
{
protected Child()
{
}
protected Delete()
{
}
public PublicPropertyToExpose()
{
}
}
I'm curious as to what is good practice when it comes to certain scenarios involving nested types in .NET.
Lets say you have a Wheel class, and the Wheel class holds Bearing objects. A Bearing object only makes sense within a Wheel and you do not want to allow it to be created independently, so it would make sense to have the Bearing class nested inside the Wheel object. However, lets say you have a scenario where you now need to read a Wheel.Bearings property outside of the Wheel class. This would now require making the nested Bearing class public.
In this situation, what is the best option?
1 - Create a public Bearing class nested within the Wheel class
2 - Create an independent Bearing class which takes a Wheel object in its constructor
3 - Create a Wheel namespace and create an independent Bearing class inside this namespace.
4 - Something else?
UPDATE:
I'm updating this with more details and to reflect some of the suggestions so far. ClassParent is the parent class, ClassChild is the child class. ClassChild is ALWAYS a child of ClassParent, it does not make sense to exist on its own. The problem is that ClassChild has a few properties that need to be exposed publically, while all the rest should only be called from ClassParent. An example is a ClassChild.Delete function which should not be exposed publically because it should only be called from ClassParent as ClassParent needs to perform appropriate cleanup and modifications.
After reviewing the suggestions, the design I've come up with looks a little unclean to me so I thought I'd ask for input. I have:
public class Parent
{
ChildNested childObj
public DeleteChild()
{
//expose this method publically
childObj.DeleteChild()
//other functionality
}
public Child GetChild()
{
//expose Child, not ChildNested publically
return childObj
}
private class ChildNested:Child
{
public Child()
{
Base.Child()
}
public DeleteChild()
{
Base.Delete()
}
}
public abstract class Child
{
protected Child()
{
}
protected Delete()
{
}
public PublicPropertyToExpose()
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里最好的设计是创建一个带有
内部
构造函数的公共Bearing
类,并在Wheel
类中创建它的实例。如果
Bearing
类需要访问Wheel
类的私有成员,您可以将公共Bearing
类设为abstract
,然后将具体实现作为Wheel
内的private
嵌套类。一般来说,您不应公开
/code> 嵌套类型
。
The best design here is to create a public
Bearing
class with aninternal
constructor, and create instances of it within theWheel
class.If the
Bearing
class needs to access private members of theWheel
class, you can make the publicBearing
classabstract
, then make a concrete implentation as aprivate
nested class inside ofWheel
.In general, you should not make
public
nested types.我想独立测试“轴承”,所以我会选择第二个选项。
I would like to test "Bearing" independently, so I would go for the 2nd option.
轴承可以单独存在,因为它可能足够有用,可以在世界其他地方(车轮之外)使用。
Wheel是通过Bearings聚合的,但是Wheel并没有定义Bearing。轴承很有用,除了车轮之外,还可以用于其他用途。
事实上,您需要 Bearing 的公共属性,这表明它需要是 Wheel 之外的公共类。
另外,当你重新发明轮子时会发生什么(我们都这样做......)。也许您使用新的“空气轴承”,例如存在于微型涡轮机中,现在您在 Wheel 类中有多种轴承类型,其中一些未使用。
我将 Bearing 放在同一个命名空间中,但不在 Wheel 类中。我很少发现需要内部类。通常,匿名类可以填补我需要的任何空白。
A Bearing can exist on its own, because its probably useful enough to be used elsewhere in the world, outside of a wheel.
A Wheel is aggregated by using Bearings, but a Wheel does not define a Bearing. Bearings are useful and can be used in other things besides wheels.
The fact that you need a public property for a Bearing indicates it needs to be a public class, outside of Wheel.
Also, what happens when you re-invent the Wheel (we all do...). Maybe you use a new "Air Bearing", such as exist in micro-turbines, now you have multiple Bearing types inside the Wheel class, some of which are not used.
I'd put the Bearing inside the same namespace, but not inside the Wheel class. Rarely do I find a need for inner classes. Usually an anonymous class fills in any gaps I need.