简体   繁体   中英

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:

//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. 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#. You'll have to create a new class that inherits from 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. 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. That is, if I'm understanding it as kind of an in-line class derivation.

Brian Rasmussen mentions using a delegate . 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.

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.

hope this helps. Read more here for delegates in-depth

Delegates

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