C#解决我的过载或多态性的更好方法
我正在努力写更好的代码 我有一个函数处理两种不同类型的输入,函数很长,两种类型之间的差别很小。 现在我这样写:C#解决我的过载或多态性的更好方法,c#,oop,polymorphism,overloading,C#,Oop,Polymorphism,Overloading,我正在努力写更好的代码 我有一个函数处理两种不同类型的输入,函数很长,两种类型之间的差别很小。 现在我这样写: function(typeA inputs) { ...... ...... <lots of same code> ...... ...... <small different code part> } function(typeB inputs) { ...... ...... <lots of same code> ...... ..
function(typeA inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeB inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeBase inputs)
{
......
......
<lots of same code>
......
......
if(inputs is TypeA)
{
// Do stuff here for TypeA
}
else if(inputs is TypeB)
{
// Do stuff here for TypeB
}
}
功能(类型A输入)
{
......
......
......
......
}
功能(类型B输入)
{
......
......
......
......
}
我想知道有没有更好的方法,我不需要放这么多重复的代码,
也许只要写一个函数就可以切换类型
typeA和typeB是不同的基类
当前A有5个项,B有3个项。试试这个,假设
typeA
和typeB
都继承自BaseType
基类(或接口):
SharedFunction(基本类型输入)
{
......
......
......
......
}
功能(A型输入)
{
共享功能(输入)
}
功能B(类型B输入)
{
共享功能(输入)
}
试试这个,假设typeA
和typeB
都继承自BaseType
基类(或接口):
SharedFunction(基本类型输入)
{
......
......
......
......
}
功能(A型输入)
{
共享功能(输入)
}
功能B(类型B输入)
{
共享功能(输入)
}
试试这个,假设typeA
和typeB
都继承自BaseType
基类(或接口):
SharedFunction(基本类型输入)
{
......
......
......
......
}
功能(A型输入)
{
共享功能(输入)
}
功能B(类型B输入)
{
共享功能(输入)
}
试试这个,假设typeA
和typeB
都继承自BaseType
基类(或接口):
SharedFunction(基本类型输入)
{
......
......
......
......
}
功能(A型输入)
{
共享功能(输入)
}
功能B(类型B输入)
{
共享功能(输入)
}
假设两种类型TypeA
和TypeB
都派生自同一基类,然后将函数参数泛化为基类,然后根据类型为“不同”的节提供条件逻辑,这将允许一种方法处理这两种类型,如下所示:
function(typeA inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeB inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeBase inputs)
{
......
......
<lots of same code>
......
......
if(inputs is TypeA)
{
// Do stuff here for TypeA
}
else if(inputs is TypeB)
{
// Do stuff here for TypeB
}
}
功能(类型库输入)
{
......
......
......
......
if(输入为A型)
{
//在这里为TypeA做些事情
}
else if(输入类型为B)
{
//在这里为TypeB做些事情
}
}
假设两种类型TypeA
和TypeB
都派生自同一基类,然后将函数参数泛化为基类,然后根据类型为“不同”的节提供条件逻辑,这将允许一种方法处理这两种类型,如下所示:
function(typeA inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeB inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeBase inputs)
{
......
......
<lots of same code>
......
......
if(inputs is TypeA)
{
// Do stuff here for TypeA
}
else if(inputs is TypeB)
{
// Do stuff here for TypeB
}
}
功能(类型库输入)
{
......
......
......
......
if(输入为A型)
{
//在这里为TypeA做些事情
}
else if(输入类型为B)
{
//在这里为TypeB做些事情
}
}
假设两种类型TypeA
和TypeB
都派生自同一基类,然后将函数参数泛化为基类,然后根据类型为“不同”的节提供条件逻辑,这将允许一种方法处理这两种类型,如下所示:
function(typeA inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeB inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeBase inputs)
{
......
......
<lots of same code>
......
......
if(inputs is TypeA)
{
// Do stuff here for TypeA
}
else if(inputs is TypeB)
{
// Do stuff here for TypeB
}
}
功能(类型库输入)
{
......
......
......
......
if(输入为A型)
{
//在这里为TypeA做些事情
}
else if(输入类型为B)
{
//在这里为TypeB做些事情
}
}
假设两种类型TypeA
和TypeB
都派生自同一基类,然后将函数参数泛化为基类,然后根据类型为“不同”的节提供条件逻辑,这将允许一种方法处理这两种类型,如下所示:
function(typeA inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeB inputs)
{
......
......
<lots of same code>
......
......
<small different code part>
}
function(typeBase inputs)
{
......
......
<lots of same code>
......
......
if(inputs is TypeA)
{
// Do stuff here for TypeA
}
else if(inputs is TypeB)
{
// Do stuff here for TypeB
}
}
功能(类型库输入)
{
......
......
......
......
if(输入为A型)
{
//在这里为TypeA做些事情
}
else if(输入类型为B)
{
//在这里为TypeB做些事情
}
}
假设这些方法都在同一个类中(所以没有基类),我会考虑使用FUNC的动作作为初始方法的参数,如:
public void Method(Action execute)
{
// Do stuff here...
execute.Invoke();
}
public void SubMethod1()
{
// Does stuff
}
public void SubMethod2()
{
// Does different stuff
}
Method(SubMethod1);
Method(SubMethod2);
然后可以像这样调用该方法:
public void Method(Action execute)
{
// Do stuff here...
execute.Invoke();
}
public void SubMethod1()
{
// Does stuff
}
public void SubMethod2()
{
// Does different stuff
}
Method(SubMethod1);
Method(SubMethod2);
假设这些方法都在同一个类中(所以没有基类),我会考虑使用FUNC的动作作为初始方法的参数,例如:
public void Method(Action execute)
{
// Do stuff here...
execute.Invoke();
}
public void SubMethod1()
{
// Does stuff
}
public void SubMethod2()
{
// Does different stuff
}
Method(SubMethod1);
Method(SubMethod2);
然后可以像这样调用该方法:
public void Method(Action execute)
{
// Do stuff here...
execute.Invoke();
}
public void SubMethod1()
{
// Does stuff
}
public void SubMethod2()
{
// Does different stuff
}
Method(SubMethod1);
Method(SubMethod2);
假设这些方法都在同一个类中(所以没有基类),我会考虑使用FUNC的动作作为初始方法的参数,例如:
public void Method(Action execute)
{
// Do stuff here...
execute.Invoke();
}
public void SubMethod1()
{
// Does stuff
}
public void SubMethod2()
{
// Does different stuff
}
Method(SubMethod1);
Method(SubMethod2);
然后可以像这样调用该方法:
public void Method(Action execute)
{
// Do stuff here...
execute.Invoke();
}
public void SubMethod1()
{
// Does stuff
}
public void SubMethod2()
{
// Does different stuff
}
Method(SubMethod1);
Method(SubMethod2);
假设这些方法都在同一个类中(所以没有基类),我会考虑使用FUNC的动作作为初始方法的参数,例如:
public void Method(Action execute)
{
// Do stuff here...
execute.Invoke();
}
public void SubMethod1()
{
// Does stuff
}
public void SubMethod2()
{
// Does different stuff
}
Method(SubMethod1);
Method(SubMethod2);
然后可以像这样调用该方法:
public void Method(Action execute)
{
// Do stuff here...
execute.Invoke();
}
public void SubMethod1()
{
// Does stuff
}
public void SubMethod2()
{
// Does different stuff
}
Method(SubMethod1);
Method(SubMethod2);
正如Karl提到的,如果TypeA和TypeB派生自同一个类,那么只需使用基类作为参数类型。如果没有,我将创建一个简单的接口,然后从中派生TypeA和TypeB,并将接口作为参数类型传递
public interface IMyType
{
// Properties you need both types to have
// Methods/Functions you need both types to have
}
那么,做
function(IMyType obj)
{
// Logic
}
希望能有所帮助。正如Karl提到的,如果TypeA和TypeB派生自同一个类,那么只需使用基类作为参数类型。如果没有,我将创建一个简单的接口,然后从中派生TypeA和TypeB,并将接口作为参数类型传递
public interface IMyType
{
// Properties you need both types to have
// Methods/Functions you need both types to have
}
那么,做
function(IMyType obj)
{
// Logic
}
希望能有所帮助。正如Karl提到的,如果TypeA和TypeB派生自同一个类,那么只需使用基类作为参数类型。如果没有,我将创建一个简单的接口,然后从中派生TypeA和TypeB,并将接口作为参数类型传递
public interface IMyType
{
// Properties you need both types to have
// Methods/Functions you need both types to have
}
那么,做
function(IMyType obj)
{
// Logic
}
呵呵