C # Java-like inline class extension?

I would have looked at Google / MSDN, but I have no idea what he called, so I ask here.

In Java, I seem to remember that you can do it really cool, for example:

Class MyClass
{
  int number;

  MyClass() { }

  void setNumber(int number)
  {
    this.number = number;
  }
}

and then do something like:

MyClass myClass = new MyClass()
  {
    override void setNumber(int Number)
    {
      this.number = 2 * number;
    }
  };

... or something like that. Forgive any mistakes I made above - I actually did not touch Java after about 6 years.

The thing is, I remember that you could pseudo-extend the inline class.

Right now I need to expand the C # WinForms control, but I need to use it only once, and the changes are very minor. All I have to do is override the CreateParamsand property OnPaint().

, , , .Net, .

inline-extension #, Java? , ? ( , MSDN?)

+3
3

( ) # 3/4.

, . "" # ( , , "NeedDataSource" ) - , - , .

: var x = new { P = 1, };, . /lambdas/ .

.

+5

, , #, #, ?

new EffectClause("Sleep Clause", SleepEffect.class)
{
    public String getClauseDescription()
    {
        return "Bla bla bla";
    }

    public boolean isEnabledByDefault()
    {
        return true;
    }
};
+1

, #, . , , :

public static void setNumber(int Number, MyClass target)
{
    target.number = 2 * number;
}

, MyExtensions; # , , any.setNumber(x);

I am not 100% sure if this allows you to override a method that already exists in the class. Another option is to inherit from the target class a class specifically designed to override the target method.

0
source

All Articles