如何在C#中实现模板等功能? 例如,FuncA,FuncB和FuncC函数使用相同的参数,然后这些函数始终只对ClassA执行相同的操作。 即使这些函数采用ClassB或三个整数,这些函数也会将ClassB或三个整数转换为ClassA,以执行与将ClassA作为参数时相同的操作。 我不确定是否有好方法,但如果你知道,请告诉我。
void FuncA(ClassA a)
{
// do something
}
void FuncA(ClassB b)
{
FuncA(b.ToA());
}
void FuncA(int x, int y, int z)
{
FuncA(new ClassA(x, y, z));
}
void FuncB(ClassA a)
{
// do something
}
void FuncB(ClassB b)
{
FuncB(b.ToA());
}
void FuncB(int x, int y, int z)
{
FuncB(new ClassA(x, y, z));
}
void FuncC(ClassA a)
{
// do something
}
void FuncC(ClassB b)
{
FuncC(b.ToA());
}
void FuncC(int x, int y, int z)
{
FuncC(new ClassA(x, y, z));
}
// FuncD, FuncE and so on...
答案 0 :(得分:1)
我想到了几个选择......
您可以创建基类:
public abstract class ActionerOnA
{
public abstract void Execute(A a);
public void Execute(B b) => Execute(b.ToA());
public void Execute(int x, int y, int z) => Execute(new A(x, y, z));
}
public class Action1 : ActionerOnA
{
public override void Execute(A a)
{
DoSomethingHereWith(a);
}
// no need to worry about the other overloads
}
您可以将其用于:
var actionerOnA = new Action1();
actionerOnA(a);
actionerOnA(b);
actionerOnA(x, y, z);
您可以使用辅助类,其中依赖项将移至构造函数而不是方法:
public class ActionerOnA
{
private A _a;
private Action<A> _actionToExecute;
public ActionerOnA(Action<A> action, A a)
{
_a = a;
_actionToExecute = action;
}
public ActionerOnA(Action<A> action, B b) : this(action, b.ToA()) { }
public ActionerOnA(Action<A> action, int x, int y, int z) : this(action, new A(x, y, z)) { }
}
然后您可以使用它:
var actionerOnA = new ActionerOnA(Func1, a);
var actionerOnA = new ActionerOnA(Func1, b);
var actionreOnA = new ActionerOnA(Func1, x, y, z);
actionerOnA.Execute();
public void Func1(A a) { }
答案 1 :(得分:1)
我通过使用隐式转换和元组解决了这个问题。
class ClassA
{
// members...
public static implicit operator ClassA(ClassB b)
{
return b.ToA();
}
public static implicit operator ClassA(Tuple<int, int, int> t)
{
return new ClassA(t.Item1, t.Item2, t.Item3);
}
}
void FuncA(ClassA a)
{
// do something
}
// we don't have to implement overloaded FuncA functions that take ClassB or three integers.
void FuncB(ClassA a) { /* do something */ }
void FuncC(ClassA a) { /* do something */ }
答案 2 :(得分:0)
这可能不是您想要的,但它可能会给您一些思考
public class MyBase
{
public int X { get; set; }
public int Y { get; set; }
public int Z { get; set; }
public void MethodA()
{
}
public void MethodB()
{
}
public void MethodC()
{
}
}
public class ClassA : MyBase
{
public ClassA()
{
}
public ClassA(int x, int y, int z)
{
X = x;
Y = y;
Z = z;
}
}
public class ClassB : MyBase
{
}
用法
var classA = new ClassA(23,56,324);
classA.MethodA();
classA.MethodB();
classA.MethodC();
var classB = new ClassB();
classB.MethodA();
classB.MethodB();
classB.MethodC();