接口和抽象c#继承
Interface and abstract c# inheritance
我遇到了接口问题。我希望链接一个方法,该方法从实现接口的抽象 class 派生其可链接方法。
public interface IBaseInterface {
public IBaseInterface ChainableMethod()
}
public abstract AbstractClassThatHelps<T> where T: IBaseInterface n {
public T ChainableMethod() {
return (T) this;
}
}
public interface IDerived : IBaseInterface { }
public class DerivedClass : AbstractClassThatHelps<IDerived>, IDerived { }
IDerived derived = new DerivedClass();
derived.ChainableMethod().ChainableMethod();
我在这里遇到的问题:为什么不能 T
显示执行合同 IModel 时 returned?
我将如何以不同的方式解决这个问题?我希望拥有类型安全,但我被迫将所有派生的 class es 改为 return IBaseInterface 而不是它们自己的接口。
实际执行:
我们有多个模型 (DerivedClass
's),它们实现了各自的 IDerived
以进行依赖注入。这些需要助手,因为我不想重复我自己。所以我们使用 AbstractClassThatHelps
作为基础,但是因为我们正在处理可链接的方法,我们需要这个基础 class 来知道要做什么 return ,因此因此泛型。 IBaseInterface
可以看作 IModel
。例如,ChainableMethod
可以看作 GetAll()
。
您可以 return T
的 实例 ,但 return 类型 不能是 T
但必须是 IBaseInterface
因为这是界面所要求的。
为了让下面的代码工作 AbstractClassThatHelps<T>
必须实现 IBaseInterface
。你怎么能returnthis
,如果this
不是IBaseInterface
public abstract AbstractClassThatHelps<T> where T: IBaseInterface n{
public T ChainableMethod(){
return this;
}
}
编辑:我不是这个设计解决的用户,但这是我对你想要实现的目标的尝试→
public interface IBaseInterface
{
IBaseInterface ChainableMethod();
}
public abstract class AbstractClassThatHelps<T>:IBaseInterface where T : IBaseInterface{
public T ChainableMethod()
{
IBaseInterface i = this;
return (T)i.ChainableMethod();
}
IBaseInterface IBaseInterface.ChainableMethod()
{
return this;
}
}
public class Concrete : AbstractClassThatHelps<Concrete>
{
}
这是有效的,你的代码充满了语法错误:
public interface IBaseInterface
{
IBaseInterface ChainableMethod();
}
public abstract class AbstractClassThatHelps : IBaseInterface
{
public IBaseInterface ChainableMethod()
{
return this;
}
}
public interface IDerived : IBaseInterface
{
}
public class DerivedClass : AbstractClassThatHelps, IDerived
{
}
internal static class Program
{
public static void Main()
{
IDerived derived = new DerivedClass();
derived.ChainableMethod().ChainableMethod();
}
}
你也可以试试这个:
public interface IBaseInterface
{
IBaseInterface ChainableMethod();
}
public abstract class AbstractClassThatHelps<T> : IBaseInterface where T : class, IBaseInterface
{
public T ChainableMethod()
{
return this as T;
}
IBaseInterface IBaseInterface.ChainableMethod()
{
return ChainableMethod();
}
}
public interface IDerived : IBaseInterface
{
IDerived Hello();
}
public class DerivedClass : AbstractClassThatHelps<IDerived>, IDerived
{
public IDerived Hello()
{
Console.WriteLine("Hello");
return this;
}
}
internal static class Program
{
public static void Main()
{
AbstractClassThatHelps<IDerived> derived = new DerivedClass();
derived.ChainableMethod().Hello().ChainableMethod();
}
}
我遇到了接口问题。我希望链接一个方法,该方法从实现接口的抽象 class 派生其可链接方法。
public interface IBaseInterface {
public IBaseInterface ChainableMethod()
}
public abstract AbstractClassThatHelps<T> where T: IBaseInterface n {
public T ChainableMethod() {
return (T) this;
}
}
public interface IDerived : IBaseInterface { }
public class DerivedClass : AbstractClassThatHelps<IDerived>, IDerived { }
IDerived derived = new DerivedClass();
derived.ChainableMethod().ChainableMethod();
我在这里遇到的问题:为什么不能 T
显示执行合同 IModel 时 returned?
我将如何以不同的方式解决这个问题?我希望拥有类型安全,但我被迫将所有派生的 class es 改为 return IBaseInterface 而不是它们自己的接口。
实际执行:
我们有多个模型 (DerivedClass
's),它们实现了各自的 IDerived
以进行依赖注入。这些需要助手,因为我不想重复我自己。所以我们使用 AbstractClassThatHelps
作为基础,但是因为我们正在处理可链接的方法,我们需要这个基础 class 来知道要做什么 return ,因此因此泛型。 IBaseInterface
可以看作 IModel
。例如,ChainableMethod
可以看作 GetAll()
。
您可以 return T
的 实例 ,但 return 类型 不能是 T
但必须是 IBaseInterface
因为这是界面所要求的。
为了让下面的代码工作 AbstractClassThatHelps<T>
必须实现 IBaseInterface
。你怎么能returnthis
,如果this
不是IBaseInterface
public abstract AbstractClassThatHelps<T> where T: IBaseInterface n{
public T ChainableMethod(){
return this;
}
}
编辑:我不是这个设计解决的用户,但这是我对你想要实现的目标的尝试→
public interface IBaseInterface
{
IBaseInterface ChainableMethod();
}
public abstract class AbstractClassThatHelps<T>:IBaseInterface where T : IBaseInterface{
public T ChainableMethod()
{
IBaseInterface i = this;
return (T)i.ChainableMethod();
}
IBaseInterface IBaseInterface.ChainableMethod()
{
return this;
}
}
public class Concrete : AbstractClassThatHelps<Concrete>
{
}
这是有效的,你的代码充满了语法错误:
public interface IBaseInterface
{
IBaseInterface ChainableMethod();
}
public abstract class AbstractClassThatHelps : IBaseInterface
{
public IBaseInterface ChainableMethod()
{
return this;
}
}
public interface IDerived : IBaseInterface
{
}
public class DerivedClass : AbstractClassThatHelps, IDerived
{
}
internal static class Program
{
public static void Main()
{
IDerived derived = new DerivedClass();
derived.ChainableMethod().ChainableMethod();
}
}
你也可以试试这个:
public interface IBaseInterface
{
IBaseInterface ChainableMethod();
}
public abstract class AbstractClassThatHelps<T> : IBaseInterface where T : class, IBaseInterface
{
public T ChainableMethod()
{
return this as T;
}
IBaseInterface IBaseInterface.ChainableMethod()
{
return ChainableMethod();
}
}
public interface IDerived : IBaseInterface
{
IDerived Hello();
}
public class DerivedClass : AbstractClassThatHelps<IDerived>, IDerived
{
public IDerived Hello()
{
Console.WriteLine("Hello");
return this;
}
}
internal static class Program
{
public static void Main()
{
AbstractClassThatHelps<IDerived> derived = new DerivedClass();
derived.ChainableMethod().Hello().ChainableMethod();
}
}