Reputation: 1619
the implementer class 'thefoo' takes an integer from its constructor. but the base class 'foo' can not see its constructor value 'input'. because the base ctor works first. i'm need of some way to tell the base that implementer's ctor value. without changing the base can i make passed the following failingtest?
public abstract class foo
{
int noImplementerKnowsThatValue = 0;
protected abstract int intro();
protected abstract int outtro();
public foo()
{
noImplementerKnowsThatValue += intro ();
}
public int compose ()
{
return noImplementerKnowsThatValue + outtro ();
}
}
public class theFoo : foo
{
int value;
public theFoo (int input)
{
value = input;
}
protected override int intro ()
{
return value;
}
protected override int outtro ()
{
return 100;
}
}
public class fooTest
{
public void failingTest ()
{
var foo = new theFoo (100);
int x = foo.compose ();
Debug.Assert (200 == x);
}
}
Upvotes: 1
Views: 92
Reputation: 106920
You can't. Not unless you change the base class.
Well, I suppose you could make
protected override int intro ()
{
return 0;
}
protected override int outtro ()
{
return 100 + value;
}
But I think that's not what you mean. And it doesn't solve the general case of this problem (for instance, if instead of int
there would be string
).
Upvotes: 2
Reputation: 108957
if you can change noImplementerKnowsThatValue
in base class to protected, you can do
public theFoo (int input)
{
base.noImplementerKnowsThatValue = input;
value = input;
}
I don't think there is a way to do this without changing the base class.
Upvotes: 1
Reputation: 4388
Aside from the obvious (making outtro return value + 100, instead of just 100), I cannot see any other mechanism that does not require changing the base class.
Upvotes: 1