开发者

Calling a base class' method

开发者 https://www.devze.com 2023-03-08 02:25 出处:网络
In c++ I would do class A { public: virtual void stuff() { //something } }; class B : public A public: virtua开发者_开发百科l void stuff()

In c++ I would do

class A
{
public:
    virtual void stuff()
    {
        //something
    }
};

class B : public A
public:
    virtua开发者_开发百科l void stuff()
    {
        //something2
        A::stuff() //something
    }
};

How would I do this in C#? I've tried

public void stuff()
{
    //something2
    A.stuff(); //something
}

but that doesn't work


base is the keyword for referencing your superclass in C#. Use:

base.stuff();


Use base. Like base.stuff();


Just to add to the answer above, base.stuff() works, unless it's the constructor you're trying to call in which case it is called as:

class A
{
public:
    public A(){}

};

class B : A
{
    public B() : base()
    {

    }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号