简体   繁体   中英

Equivalent to Java's “restricted” and “extends” in C#?

EDIT: I apologize, I meant protected instead of restricted . I was tired.

Is there an equivalent to Java's restricted and extends in C#? I can see that neither are in C#, and they would be useful for a current programming project.

Code:

using System;

namespace CServer.API
{
    public class Plugin
    {
        restricted Plugin ()
        {
        }
    }
}

and say a plugin did this:

using System;
using CServer.API;

namespace Whatever
{
    public class WhateverPlugin extends Plugin
    {
    }
}

I want to have a custom constructor that executes some code before the plugin's constructor.

It sounds like you probably want:

using System;

namespace CServer.API
{
    public class Plugin
    {
        protected Plugin()
        {
            // This code will execute before the body of the constructor
            // in WhateverPlugin
        }
    }
}

and

using System;
using CServer.API;

namespace Whatever
{
    // : is broadly equivalent to both implements and extends
    public class WhateverPlugin : Plugin
    {
        public WhateverPlugin() // implicitly calls base constructor
        {
            // This will execute after the Plugin constructor body
        }
    }
}

Note that restricted isn't a keyword in Java; I'm assuming you actually mean protected .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM