抽象类(我想继承派生类中的抽象成员) [英] abstract class (i want to inherit abstract member in derived class)

查看:73
本文介绍了抽象类(我想继承派生类中的抽象成员)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//this is absract class
abstract class abc
{
    //abstract member
    public string _str ="HELLO";
   }

//this is derived class
class xyz : abc
{
    public void add()
    {
        string _str = "WORLD";
//here i want hello world result in derived class method SO HOW CAN I DO THAT
        Console.WriteLine(?);
        Console.ReadLine();
    }
}

推荐答案

顺便说一句,答案是这样的:
By the way, the answer is this:
string _str = "WORLD";
System.Console.Write(_str);
System.Console.Write(base._str);



矛盾的是,这将具有相同的效果:



Paradoxically, this will have the same effect:

string _str = "WORLD";
System.Console.Write(_str);
System.Console.Write(this._str);


但这是因为没有其他_str作为类成员;新的_str是堆栈变量.考虑一下:


but this is because there is no another _str as a class member; the new _str is a stack variable. Consider this:

class xyz : abc {
    new string _str = "HIDES BASE _str";
    public void add()
{
    string _str = "WORLD";
    System.Console.Write(_str); //will print "WORLD"
    System.Console.Write(this._str); //will print "HIDES BASE _str"
    System.Console.Write(base._str); //will print "HELLO";
}



上面的关键字new仅用于防止隐藏基本_str的警告,而这种隐藏确实是不好的样式.

请了解这些简单的技术,并请他人帮助您.是的,那是一场讲座-感谢您的关注. :-)

-SA



The keyword new above is only used to prevent warning about hiding of base _str, and such hiding is indeed bad style.

Please understand these simple techniques and be considerate to people helping you. Yes, that was a lecture — thank you for attention. :-)

—SA


转到此处 [ ^ ]

就其价值而言,您提供的代码有很多错误,以至于很难知道从哪里开始指出它们.您的老师甚至告诉过您什么是抽象类,以及为什么要使用一个抽象类?
Go here [^]

For what it''s worth, there are so many things wrong with the code you presented that it''s hard to knwo where to start pointing them out. Did your instructor even tell you what an astract class is and why you would want to use one?


这篇关于抽象类(我想继承派生类中的抽象成员)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆