C# 中的派生类无法显示基类变量的值

发布于 2024-08-17 02:39:00 字数 698 浏览 5 评论 0原文

我正在尝试执行以下代码,

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);
    }
}

当我执行 aa.show() 时,我得到 5a 的值)作为输出,其中aaA 的实例,但是当我执行 bb.show() 时,其中 bbB,输出为 0a 的值)zb< 的值/代码>)。

有人可以解释为什么派生类无法显示 a 的当前值,即使它已被声明为受保护,但它能够显示 b 的正确值?

更新:

我会尝试建议的解决方案。关于编译错误,没有,我能够得到问题中提到的输出。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

怎言笑 2024-08-24 02:39:01

请尝试以下操作:

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); 
     } 
} 

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); 
     } 
} 
辞旧 2024-08-24 02:39:01

如果您在 A 类的 Show() 方法之外设置 a 的值,您将得到您期望的结果。 B.Show 的实现隐藏了 A.Show,因此它永远不会执行。

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.

唱一曲作罢 2024-08-24 02:39:01

您可能有这样的构建警告,不是吗?

警告 1“ConsoleApplication7.B.Show()”
隐藏继承的成员
'ConsoleApplication7.A.Show()'。使用
新关键字(如果隐藏)是
故意的。 C:\文件和
设置\jhoover.ANDT\我的
文档\Visual Studio
2005\Projects\ConsoleApplication7\ConsoleApplication7\Program.cs 25 21 ConsoleApplication7

正如 Paolo 提到的,B.Show 隐藏了 A.Show()。 Webleeuw 的代码展示了如何通过将 A.Show() 设为虚拟并使用 override 关键字标记 B.Show() 来执行您想要的操作。

PS 不要使用 ""+a 转换为字符串,使用 a.ToString()

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()

走过海棠暮 2024-08-24 02:39:01

bb.show()中a的当前值为0,因此输出是正确的。仅在 A 类的 Show 方法中将其设置为 5。

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.

小女人ら 2024-08-24 02:39:01

您必须在类构造函数或其他方法中初始化 a 的值。
B 类中声明的 Show() 隐藏了 A 类中声明的 Show(),因此 的方法未调用 类。

阅读有关 C++ 和 C# 中的多态性和继承的更多信息。

例如,您可能会对这段代码的结果感到惊讶:

A aa;
B bb;
//... init vars ...
aa = bb;
bb.Show();
aa.Show();
bb.Show();

;-)

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();

;-)

迷荒 2024-08-24 02:39:01

a=5;在您的 A.Show 方法中设置。

B 类永远不会设置 a=5,因为它将使用 B.Show 方法。

这个问题不太正确:为什么派生类无法显示 a 的当前值

派生类能够显示a 值,但是这个a 值从未改变B 类中的默认值0。

还尝试初始化 Show() 的外部,例如:

class A {
    protected int a = 5; // a = 5 will be visible in B too

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
当爱已成负担 2024-08-24 02:39:01

使用模板方法模式 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;
 }
}

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;
 }
}
乖乖公主 2024-08-24 02:39:00

您的 B 方法隐藏了 A 方法,您应该收到有关此的编译器警告。

更好的是如下 - 请注意 virtual 和 override 关键字:

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);
    }
}

但是在这里您将弹出两个消息框。您需要将 A 中变量的设置与消息框函数分开,例如在构造函数中设置 'a'。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文