简体   繁体   English

如何在C#中的对象实例中覆盖一个方法

[英]How to override a method in the instantion of an object in C#

I am a Java programmer trying to transition to C# and I'm hoping there's a way to do something in C# that I'm accustomed to in Java: overriding a method in the declaration of an abstract object like so: 我是一名Java程序员,试图转换到C#,我希望有一种方法可以在C#中做一些我在Java中习惯的方法:在抽象对象的声明中覆盖一个方法,如下所示:

//This is the way I do it in Java and want to do in C#
Keyword k = new Keyword("quit"){
    public abstract void do(String context){
        //TODO Do stuff
    }
};

This is for some text game stuff I've been doing for a while in Java. 这是一些文字游戏的东西,我在Java中已经做了一段时间。 I've looked into abstract and virtual and anonymous classes but none of them do exactly this. 我已经研究过抽象的,虚拟的和匿名的类,但是没有一个完成这个。 Abstract and virtual want me to create a whole new subclass, but this would be time consuming and unfeasible on a large scale. 抽象和虚拟想要我创建一个全新的子类,但这将耗费大量时间并且不可行。 Anonymous classes don't (as far as I can tell) enable me to override methods, just fields and don't provide any stabilization for me to rely on. 匿名类没有(据我所知)让我覆盖方法,只是字段,并没有为我提供任何稳定依赖。

If there is a way to do this or something similar please explain. 如果有办法做到这一点或类似的东西请解释。 Thanks for your time. 谢谢你的时间。

That doesn't work in C#. 这在C#中不起作用。 You'll have to create a new class that inherits from Keyword. 您必须创建一个继承自Keyword的新类。

public class MyKeyword : Keyword
{
    public MyKeyword(string s) : base(s)
    { }

    public override void do(string context)
    {
        // TODO: Do stuff.
    }
}

Anonymous Types in C# aren't classes that you can provide any public methods for. C#中的匿名类型不是您可以为其提供任何公共方法的类。 They only have properties, and are intended to be a quick, intra-method way of pasing complex data from one line to the next. 它们只有属性,并且是一种快速的方法内方法,可以将复杂数据从一行变为另一行。

To be honest, I didn't know you could do what you show in Java. 说实话,我不知道你可以做你在Java中展示的东西。 That is, if I'm understanding it as kind of an in-line class derivation. 也就是说,如果我将其理解为一种内联类派生。

Brian Rasmussen mentions using a delegate . Brian Rasmussen提到使用代表 That would look something like this: 这看起来像这样:

public delegate void DoSomething(string context);
public class Keyword
{
    public DoSomething Do;

    private void CallsDo()
    {
        if (Do != null)  Do("some string");
    }
}

Then you can assign to it: 然后你可以分配给它:

Keyword k = new Keyword();
k.Do = (ctx) => { /* Do something with ctx string */ };

代表们可能就是你所追求的。

You can utilize a delegate for this approach: Note the example 您可以使用委托来实现此方法:请注意示例

public class Keyword
{
     public delegate void Do();
}

//Area of Execution
{
   //...
   Keyword k = new Keyword();
   k.Do = delegate() 
   {
       Console.Writeln("Anonymous Inner function assigned to a callback function i.e a Delegate!");  
   };
}

These are much like function pointers in C/C++ but that may mean nothing to you depending on your background. 这些与C / C ++中的函数指针非常相似,但根据您的背景,这可能对您没有任何意义。

A delegate is, in the simplest terms, a type-safe object that encapsulates a method/function. 从最简单的角度来说,委托是一个封装方法/函数的类型安全对象。 What this means is that it maintains a reference to the method or methods and can invoke them later through the delegate object rather than explicitly on the method(s) themselves. 这意味着它维护对方法或方法的引用,并且可以稍后通过委托对象而不是显式地在方法本身上调用它们。 You can assign an anonymous function to the right hand side much the same as you can to a method in Java as you described. 您可以将右侧的匿名函数分配给Java,就像您描述的Java中的方法一样。

hope this helps. 希望这可以帮助。 Read more here for delegates in-depth 在此深入了解代表们

Delegates 代表

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

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