c# - Implement base method, yet still provide own implementation? -
there 2 strings parse:
param1=value1;param2=value2;param3=value3;
and...
param1=value1;param4=value4;param999=value999;
every parameter has parsed different because contain complex information of different types. in glory of holy dry paradigm, seems appropriate parsing of 'param1
' done once. base class implementation may this:
public abstract class mybase { public complexobj myparam1 { get; set; } public abstract static mybase parse(string mystring) { // sinful witchcraft parses value // of param1 complexobj myparam1 // , returns instance of mybase } }
and derived class may following:
public class myderived : mybase { public complexobj2 myparam2 { get; set; } public complexobj3 myparam3 { get; set; } public static myderived parse(string mystring) { params = mystring.split('params/values') base.parse(params[0]); myparam2 = magicparseparam2[1]; myparam3 = magicparseparam3[2]; } }
now, doesn't work because declared parse()
method of base class abstract, forcing every derived class provide own implementation of method. in particular case, don't want write parsing of param1
every implementation.
how can solve problem?
make base method virtual rather abstract. can call base.parse(string) in implementing class.
edit: oh , don't make them static. looks want doing want creating instance of class.
example: http://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx
Comments
Post a Comment