I have a class need to use a object, and the object is constraint of some kind of class. However, I have two design for this method to implement: generic and polymorphism, then the example code like below:
class S3 : S{} class S2 : S{} class S{} class C : A{ public override void DoOne(S s){ var tmp = s as S3; } public override void DoTwo<T>(T s){ var tmp = s as S3; } } class B : A{ public override void DoOne(S s){ var tmp = s as S2; } public override void DoTwo<T>(T s){ var tmp = s as S2; } } abstract class A{ public abstract void DoOne(S s); public abstract void DoTwo<T>(T s) where T : S; } var s2 = new S2(); var b = new B(); b.DoOne(s2); b.DoTwo(s2);
The class B
need use S2
object, and maybe I have class C
need use S3
object. I don't know what's better for implement those.
没有评论:
发表评论