我在NHibernate的類定義發生在此:C#不尋常的繼承語法W /仿製藥
public class SQLiteConfiguration : PersistenceConfiguration<SQLiteConfiguration>
所以這個類是從由...派生類參數基類繼承? 我的頭剛爆炸。
有人可以解釋這是什麼意思,以及這種模式如何有用嗎?
(這不是一個特定的NHibernate-問題,順便說一句。)
我在NHibernate的類定義發生在此:C#不尋常的繼承語法W /仿製藥
public class SQLiteConfiguration : PersistenceConfiguration<SQLiteConfiguration>
所以這個類是從由...派生類參數基類繼承? 我的頭剛爆炸。
有人可以解釋這是什麼意思,以及這種模式如何有用嗎?
(這不是一個特定的NHibernate-問題,順便說一句。)
開發一個雙鏈接樹時,我已經使用了相同的模式。每個節點都有1個父,和0-許多孩子
class Tree<T> where T : Tree<T>
{
T parent;
List<T> children;
T Parent { get; set; }
protected Tree(T parent)
{
this.parent = parent;
parent.children.Add(this);
}
// lots more code handling tree list stuff
}
實施
class Coordinate : Tree<Coordinate>
{
Coordinate(Coordinate parent) : this(parent) { }
static readonly Coordinate Root = new Coordinate(null);
// lots more code handling coordinate systems
}
使用
Coordinate A = Coordinate.Root;
Coordinate B = new Coordinate(A);
B.Parent // returns a Coordinate type instead of a Node<>.
所以任何來自Tree<>
繼承將包含父母和性能的孩子中的對象適當的類型。這個技巧對我來說是純粹的魔術。
這是http://stackoverflow.com/questions/3783321/why-does-this-generic-constraint-compile-when-it-seems-to-have-a-circular-referen/3789193# 3789193 – 2011-01-08 05:27:37
埃裏克:我在蘭伯特的評論中引用了這個問題。 – anon 2011-01-08 05:33:11
我很早以前就讀過這個主題,但現在才知道這個特定的場景。我再次通過搜索「我的頭部爆炸」找到該線程。大聲笑。你寫的好東西,否則我可能再也找不到它了。 :-) – 2011-04-08 20:44:50