本文介绍了在哪里以及如何使用嵌套类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果一个类只能在另一个类实例化,因此是正确的使用它嵌套在class.I认为这将有助于我们很好的design.When我看看我的项目,我几乎从来没有见过这样嵌套structure.But如果我尝试嵌套类,所以这时候另一个问题出现在我的mind.For例如

I am thinking that if a class will be instantiated only in another class so it is right to use it nested in that class.I think this will help us good design.When i look at my project i have almost never seen such nested structure.But if i try to nested classes so this time another questions appear in my mind.For example

我有板类,移动类,如ShortCastle,LongCastle,EnPassant,促进和像典当,王后,车,马等,所以很显然板类将实例件类和片类将实例移动类件。对于一个优秀的设计,促进招类中嵌套典当应为,因为只有典当行可以促进itself.short和长城堡应该被嵌套王,因为只有国王可以有这种类型的动作。

I have Board class, Move classes such as ShortCastle,LongCastle,EnPassant,Promote and Pieces like Pawn,Queen,Rook,Knight etc. So it is clear Board classes will instantiate Piece classes and Piece classes will instantiate Move classes. For a good design,Promote move class should to be nested of Pawn because only pawn can promote itself.short and long Castles should to be nested of King because only king can have such type moves.

试图把所有的片类到局级不看良好的设计,因为8-9级将成为董事会类里面,它会真的很烦人一体板类文件会太大,几乎readable.I preFER让每件类在另一个文件中。好的,我们可以创建部分局级,但仍然不是很麻烦8-9部分局级文件将保存每件类?它是最好不要让他们拼图?同约饮片创建另一个局部件文件只是另一个移动类型的类?如果嵌套类只是拿小空间,所以它不会有任何问题,但如果它需要很多方法?

Trying to put all Piece classes into Board class is not looking good design because 8-9 class will be inside of Board class and it will really annoying one Board class file will be too large and hardly readable.I prefer keep each piece class in another file. Good that we can create partial Board class but still isn't it annoying 8-9 Partial Board class files will hold each piece class? Is it better to not make them nested ? Same about Pieces Create another partial Piece file just for another Move type class ? If nested class just take small space so it wouldn't be any problem but if it takes many methods ?

推荐答案

我觉得你太慷慨嵌套类。看一看设计指南对于嵌套类型。

I think you are too generous with nested classes.Have a look at this design guideline for nested types.

不要使用嵌套的类型,如果满足下列条件:

Do not use nested types if the following are true:

  • 的类型必须被实例化客户端code。如果一个类型有一个公共构造函数,它可能不应该嵌套。这样做的理由准则的是,如果一个嵌套类型可以被实例化时,表示该类型在一个地方库自身。您可以创建它,使用它,并摧毁它不使用外部类型。因此,它不应该被嵌套。内部类型不应该被广泛地重新使用以外外型没有的关系到外类型。
  • 引用类型通常是在客户端code声明。
  • The type must be instantiated byclient code. If a type has a publicconstructor, it probably should notbe nested. The rationale behind thisguideline is that if a nested typecan be instantiated, it indicatesthat the type has a place in thelibrary on its own. You can createit, use it, and destroy it withoutusing the outer type. Therefore, itshould not be nested. An inner typeshould not be widely reused outsideof the outer type without arelationship to the outer type.
  • References to the type are commonlydeclared in client code.

的碎片可能属于一个板(计件集合作为一个部件?),但可以共存而不它。你可能会F.E.想重用板无片(主题等),并重新使用部分无板(位置等)。

The pieces may belong to a board(Piece-collection as a member?) but could coexist without of it. You might f.e. want to reuse boards without pieces(themes etc) and also reuse pieces without a board(positions etc).

这篇关于在哪里以及如何使用嵌套类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 10:33
查看更多