derived class cannot display value of base class variables in C#
I am trying to execute the following code
class A
{
protected int a;
protected char b;
public void Show()
{
a=5;
MessageBox.Show(""+a);
}
}
class B:A
{
public void Show()
{
b='z';
开发者_如何学运维 MessageBox.Show(""+a+ ""+b);
}
}
I am getting 5
(value of a
) as the output when I do aa.show()
where aa
is the instance of A
but when I do bb.show()
, where bb
is the instance of B
, the output comes out as 0
(value of a
) z
(value of b
).
Can someone please explain why the derived class is unable to display the current value of a even though it has been declared as protected whereas it is able to display the correct value of b
?
Update:
I'll try the solutions suggested. Regarding the compilation error, there was none and I was able to get the output as mentioned in the question.
Your B method is hiding the A method, you should be getting a compiler warning about this.
Better is as follows - note the virtual and override keywords:
class A
{
protected int a;
protected char b;
public virtual void Show()
{
a=5;
MessageBox.Show(""+a);
}
}
class B:A
{
public override void Show()
{
base.Show();
b='z';
MessageBox.Show(""+a+ ""+b);
}
}
But here you'll have two message boxes pop up. You need to separate the setting of the variable in A with the message box function, for example set 'a' in the constructor.
Try the following:
class A
{
protected int a;
protected char b;
public virtual void Show()
{
a=5;
MessageBox.Show(""+a);
}
}
class B:A
{
public override void Show()
{
b='z';
MessageBox.Show(""+a+ ""+b);
}
}
If you set the value of a outside of the Show() method in class A, you will get the result that you expect. The implementation of B.Show hides A.Show, so it never executes.
You probably have a build warning like this, don't you?
Warning 1 'ConsoleApplication7.B.Show()' hides inherited member 'ConsoleApplication7.A.Show()'. Use the new keyword if hiding was intended. C:\Documents and Settings\jhoover.ANDT\My Documents\Visual Studio 2005\Projects\ConsoleApplication7\ConsoleApplication7\Program.cs 25 21 ConsoleApplication7
As Paolo mentions, B.Show is hiding A.Show(). Webleeuw's code shows how to do what you want, by making A.Show() virtual and marking B.Show() with the override keyword.
P.S. Don't use ""+a to convert to string, use a.ToString()
Current value of a in bb.show() is 0, so the output is correct. You set it to 5 only in the Show method in the A class.
Your must initialize value of a
in class constructor or other method.
Show()
declared in B
class hides Show()
declared in A
class, therefore method of A
class not invoked.
Read more about polymorphism and inheritance in C++ and C#.
E.g. you can be surprised with results of such piece of code:
A aa;
B bb;
//... init vars ...
aa = bb;
bb.Show();
aa.Show();
bb.Show();
;-)
a=5; is set in your A.Show method.
B class will never set a=5 because it will use the B.Show method.
The question is not quite correct: why the derived class is unable to display the current value of a?
The derived class is able to display the a value, but this a value never changed from default 0 in the B class.
try also to initialize a outside of Show() like:
class A {
protected int a = 5; // a = 5 will be visible in B too
Use a Template Method pattern http://en.wikipedia.org/wiki/Template_method_pattern
class A
{
protected int a;
protected virtual string GetForShow()
{
a = 5;
return a.ToString();
}
public void Show()
{
var forShow = GetForShow();
MessageBox.Show(forShow);
}
}
class B
{
protected char b;
protected override string GetForShow()
{
b = 'z';
return base.GetForShow() + b;
}
}
精彩评论