[英]c# return this for generic return type method
類中的許多方法都將return this;
這樣我就可以在一行中調用多個函數。 類似a.method1().method2().method3();
當我嘗試做一個基類時出現問題。 如果我創建了一個在基類中返回T
類型的方法,那么我將無法再return this;
因為基類類型不是T
我不能只將返回類型作為基類,因為繼承的類上還有許多不在基類中的方法。 我怎么解決這個問題?
樣例代碼:
public class baseClass<T>
{
public T method1()
{
//Do stuffs
return this;//doesnt work
}
}
public class inheritedClass:baseClass<inheritedClass>
{
public inheritedClass method2()
{
//Do stuffs
return this;
}
}
基類的返回類型應該是基類本身,而不僅僅是T。
public class baseClass<T>
{
public baseClass<T> method1()
{
//Do stuffs
return this;
}
}
在您的基類示例中,“ this”不是“ T”的類型,而是“ baseClass {T}”的類型。 這就是為什么它不起作用。 我不確定您要在這里完成什么,但這可能會編譯...
public class baseClass<T>
{
public baseClass<T> method1()
{
return this;
}
}
public class inheritedClass : baseClass<inheritedClass>
{
public baseClass<inheritedClass> method2()
{
return this.method1();
}
}
編輯:我現在明白你的問題。 這可能是比使用繼承更好的整體方法。 如果需要,可以將接口轉換為通用接口。
public interface FluentStuff
{
FluentStuff method1();
FluentStuff method2();
}
public class MyClass : FluentStuff
{
public FluentStuff method1()
{
return this;
}
public FluentStuff method2()
{
return this;
}
}
但是如果您堅持使用繼承...
public interface FluentStuff
{
FluentStuff method1();
FluentStuff method2();
}
public abstract class BaseClass : FluentStuff
{
public virtual FluentStuff method1()
{
return this;
}
public abstract FluentStuff method2();
}
public class MyClass : BaseClass, FluentStuff
{
public override FluentStuff method2()
{
return this;
}
}
我極力主張在繼承方面采用合成。
泛型示例...
public interface FluentStuff<T>
{
FluentStuff<T> method1();
FluentStuff<T> method2();
}
public abstract class BaseClass<T> : FluentStuff<T>
{
public virtual FluentStuff<T> method1()
{
return this;
}
public abstract FluentStuff<T> method2();
}
public class MyClass : BaseClass<MyClass>, FluentStuff<MyClass>
{
public override FluentStuff<MyClass> method2()
{
return this;
}
}
您發布的另一個問題/關注的最終示例...
public class SharedFunctionality
{
public void DoStuff1()
{
// common implementation for do stuff 1
}
public void DoStuff2()
{
// common implementation for do stuff 2
}
}
public class MyClass1
{
private readonly SharedFunctionality sharedFunctionality;
public MyClass1()
{
this.sharedFunctionality = new SharedFunctionality();
}
public MyClass1 Method1()
{
this.sharedFunctionality.DoStuff1();
return this;
}
public MyClass1 Method2()
{
this.sharedFunctionality.DoStuff2();
return this;
}
}
public class MyClass2
{
private readonly SharedFunctionality sharedFunctionality;
public MyClass2()
{
this.sharedFunctionality = new SharedFunctionality();
}
public MyClass2 Method1()
{
this.sharedFunctionality.DoStuff1();
return this;
}
public MyClass2 Method2()
{
this.sharedFunctionality.DoStuff2();
return this;
}
public MyClass2 Method3()
{
// do something only this class does
return this;
}
}
class Program
{
static void Main(string[] args)
{
MyClass1 c1 = new MyClass1();
c1.Method1().Method2();
MyClass2 c2 = new MyClass2();
c2.Method1().Method2().Method3();
}
}
可能是這個
public abstract class BaseClass<T> where T : BaseClass<T>
{
public T Method1()
{
//Do stuffs
// We are sure any instance of this class is T : BaseClass<T>.
// Only exception might be direct instance of BaseClass<T> and that's why we made BaseClass abstract.
return (T)this;
}
}
public class InheritedClass : BaseClass<InheritedClass>
{
public InheritedClass Method2()
{
//Do stuffs
return this;
}
}
兩件事發生了變化。 首先,我們仍在強制轉換,但在基類中執行此操作。 其次,我們保證此轉換可以在約束和抽象的地方使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.