c# - Differentiate between base and derived class when passing as parameters to overloaded methods -
c# - Differentiate between base and derived class when passing as parameters to overloaded methods -
i'm implementing visitor pattern in c# lecture in university. works fine @ moment, have question task had do. @ moment have this:
public class treestructure<t> { protected treestructure<t> _left; protected treestructure<t> _right; public t myvalue; public treestructure(treestructure<t> left, t value ,treestructure<t> right) { this._left = left; this._right = right; this.myvalue = value; } public virtual void inorder(myvisitor<t> visitor) { if(!isempty(this._left)) { _left.inorder(visitor); } visitor.visit(this); if(!isempty(this._right)) { _right.inorder(visitor); } } protected bool isempty(treestructure<t> node) { homecoming node == null; } } public class specialtree<t> : treestructure<t> { public specialtree(treestructure<t> left, t value, treestructure<t> right) { this._left = left; this._right = right; this.myvalue = value; } public override void inorder(myvisitor<t> visitor) { if(!isempty(this._left)) { _left.inorder(visitor); } visitor.visit(this); console.writeline("hallo"); if(!isempty(this._right)) { _right.inorder(visitor); } } }
and should implement visitors. no problem, until should differentiate in 1 visitor type of tree. have 1 visitor looking this:
public interface myvisitor<t> { void visit(treestructure<t> tree); } public class countvisitor<t> : myvisitor<t> { public int count { get; set;} public countvisitor() { count = 0; } public void visit(treestructure<t> tree) { count++; } public void visit(specialtree<t> specialtree) { count+=2; } }
so question or problem have is, apparently works in java, not in c#. visit()
method specialtree
is never called. know can check type of object here: what best way differentiate between derived classes of base of operations class? can explain me, why doesn't work overloaded methods? or have error here?
edit: here 1 test initialisation of tree:
treestructure<int> tree1 = new treestructure<int>(new treestructure<int> (null,3,null),1,new specialtree<int>(null,2,null)); countvisitor<int> visitor2 = new countvisitor<int>(); tree1.inorder(visitor2); console.writeline("nodecount: " + visitor2.count.tostring());
this highly dependant on implementation of myvisitor<t>
. in comment interface. sure whole example? code show in question not compile because specialtree<t>
not invoke parent constructor.
if myvisitor<t>
not declare visit(specialtree<t> specialtree)
, interface dispatch done inorder
never reach specialtree
case.
update
considering updated initialization code, , assuming interface declared this:
interface myvisitor<t> { void visit(treestructure<t> treestructure); void visit(specialtree<t> tree); }
your countvisitor<t>
gives me result of 4, expected.
why didn't work?
i'll seek explain why illustration did not work. method inorder
in class specialtree
has parameter of type myvisitor
. when method visit
called on myvisitor
, there no overload takes specialtree
, however, there overload takes treestructure
. c# not consider implementation of interface when looking right method call. therefore, method has treestructure
parameter invoked instead of method accepts specialtree
parameter.
what if don't care kind of tree construction is?
in response comment, can create method called implementations of tree:
class donotcarevisitor<t> : myvisitor<t> { void genericvisit(treestructure<t> tree) { //.. whatever } public void visit(treestructure<t> tree) { genericvisit(tree); } public void visit(specialtree<t> tree) { genericvisit(treestructure); } }
c# visitor
Comments
Post a Comment