C#类Java类内嵌延期? [英] C# Java-like inline extension of classes?
问题描述
我想看看这个在谷歌/ MSDN,但我不知道它叫什么,所以我问在这里。
在Java中,我似乎记得你可以这样做很酷的事情,如:
MyClass类
{
INT编号;
MyClass的(){}
无效setNumber(INT数)
{
this.number =数字;
}
}
,然后做一些事情,如:
MyClass的MyClass的=新MyClass的()
{
覆盖无效setNumber(INT编号)
{
this.number = 2 *号;
}
};
......什么的。请原谅我上面犯任何错误 - 我还没有真正触及约6年的Java
问题的关键是,我记得你可以伪扩展类的内联<。 / p>
现在,我需要延长一个C#的WinForms控制,但我只需要使用一次,而修改是非常小的。所有我需要做的是重写的CreateParams
属性和的OnPaint()
处理程序。
我的解决方案是所有的地方已经越来越庞大的带班,这似乎是一个耻辱,包括另一类基本上是相同的标准.NET控件,只是非常轻微的不同的行为。
是否有可能做到这一点的直列扩展在C#中像您在Java可能吗?如果是这样,怎么样? (什么叫这样我就可以看看它的MSDN?)
这(明确的主格匿名类型)不可能在C#中四分之三。
类型必须明确创建,然后构建。任务会被倒在C#中的使用事件和委托(类调用事件,提供实施,如NeedDataSource」)之 - 的可以说正因为如此,虽然它很有道理在许多情况下
如果有可能创建明确的非主格类型:变种x =新{P = 1,};
,但只在局部范围。隐式的方法包括委托/ lambda表达式/匿名函数,并不适用于此。
编码愉快。
I would look this up on Google/MSDN, but I have no idea what it's called so I'm asking here.
In Java, I seem to remember you can do this really cool thing like:
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. Forgive any mistakes I made above - I haven't actually touched Java in about 6 years.
The point is, I remember you could pseudo-extend a class inline.
Right now, I need to extend a C# WinForms control, but I only need to use it once, and the modifications are very minor. All I need to do is to override the CreateParams
property and OnPaint()
handler.
My solution is already getting huge with classes all over the place, it seems like a shame to include yet another class which is basically identical to a standard .Net control, just with very slightly different behaviour.
Is it possible to do this inline-extension in C# like you could in Java? If so, how? (and what is it called so I can look it up on MSDN?)
This (explicit nominative anonymous types) is not possible in C#3/4.
The types must be created explicitly and then constructed. Tasks are sometimes "inverted" in C# with the use of Events and Delegates (class invokes Event which supplies implementation such as "NeedDataSource") -- arguably because of this, although it just makes sense in many cases.
If is possible to create explicit non-nominative types: var x = new { P = 1, };
but only in a local scope. Implicit methods include delegates/lambdas/anonymous functions and do not apply here.
Happy coding.
这篇关于C#类Java类内嵌延期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!