简体   繁体   English

相当于Java在C#中的“受限”和“扩展”?

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

EDIT: I apologize, I meant protected instead of restricted . 编辑:我很抱歉,我的意思是protected而不是restricted I was tired. 我好累

Is there an equivalent to Java's restricted and extends in C#? 是否有等效于Java的restricted并在C#中extends I can see that neither are in C#, and they would be useful for a current programming project. 我可以看到它们都不在C#中,它们对于当前的编程项目很有用。

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; 请注意, restricted并不是Java中的关键字。 I'm assuming you actually mean protected . 我假设您实际上是在说protected

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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