I have a base class that looks as follows
public class base
{
public int x;
public void adjust()
{
t = x*5;
}
}
and a class deriving from it. Can I set x’s value in the derived class’s constructor and expect the adjust() function to use that value?
Yes, that should work entirely as expected, even though your code sample does not quite make sense (what is
t?). Let me provide a different exampleIf we use
Base:…and if we use
Derived:One thing to note though is that if
xis used in theBaseconstructor, setting it in theDerivedconstructor will have no effect:In the above code sample,
GetValuewill return7for bothBaseandDerived.