多态和泛型 [英] polymorphism and Generics

查看:168
本文介绍了多态和泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


多态性是否可以与泛型一起使用?如果可以,可以给我一个真实的示例,说明使用多态性的真实代码,当我们谈论多态性时,ArrayList和Array有什么区别
感谢

Hi,
is polymorphism is allowed to used with generics ? if yes can give me a real example real code used polymorphism and what is the difference between ArrayList and Array when we talk about Polymorphism
thanks

推荐答案

多态性不是可以允许或禁止的现象.泛型无法改变这种情况,因为它们与多态性无关,因为它们与继承,虚拟"和后期绑定无关.泛型是静态的;在运行时可以实例化的任何类型都不是通用的;在构造实例(对象)之前,将其实例化为最终类型.由泛型创建的泛型类型和最终类型始终是编译时类型.相反,在运行时,所有虚拟"行为和延迟绑定都会在实例及其运行时类型上播放.

这样,泛型和多态是并行的现象,不会相互干扰.因此,它们可以以任何可考虑的方式自由组合.

代码示例?周围的样本太多.

在这里,您可以找到一个适用于几乎所有面向对象语言的概念示例: http://en.wikipedia.org/wiki/Subtype_polymorphism#Examples [ ^ ].

另请参见 http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29 [ ^ ].

为什么不知道在教程中可以找到多少样本:
http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html [ ^ ],
http://home.cogeco.ca/~ve3ll/jatutor5.htm [ http://www.javaworld.com/javaworld/javatips/jw-javatip30.html [ ^ ],
http://www.javaworld.com/javaworld/jw-04-2001/jw-0413-polymorph.html [ ^ ].

毕竟,Google要提供更多服务.

—SA
Polymorphism is not a phenomena which can be allowed or prohibited. Generics cannot change the situation, because they have nothing to do with polymorphism, because they have nothing to do with inheritance, "virtual" and late binding. Generics are static; any type which can be instantiated during run-time is not generic; it is instantiated into a final type before an instance (object) is constructed. Generic types and final types created out of generics are always the compile-time types. In contrast, all "virtual" behavior and late binging is played on instances and their run-time types, during run-time.

This way, generics and polymorphism are parallel phenomena which do not intercept; therefore, they can be freely combined in any thinkable ways.

A code sample? There are too many samples around.

Here, you can find a conceptual example, good for nearly all object-oriented language: http://en.wikipedia.org/wiki/Subtype_polymorphism#Examples[^].

See also http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^].

Why knows how many samples you can find in tutorials:
http://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html[^],
http://home.cogeco.ca/~ve3ll/jatutor5.htm[^],
http://www.javaworld.com/javaworld/javatips/jw-javatip30.html[^],
http://www.javaworld.com/javaworld/jw-04-2001/jw-0413-polymorph.html[^].

After all, Google for more.

—SA


尊敬的Hamzah,

请点击此链接,它可能会对您有所帮助.

http://stackoverflow.com/questions/1814851/c-sharp-generics- and-polymorphism-an-oxymoron [ ^ ]

谢谢
Dear Hamzah,

Please follow this link it might help you out.

http://stackoverflow.com/questions/1814851/c-sharp-generics-and-polymorphism-an-oxymoron[^]

Thanks


是的,您可以创建方法的重载
例如:
公共无效GetSomething(T a)
{
做某事
}

公共无效GetSomething(T a,K b)
{
做某事
}
ye you can create overload of a method
Eg:
public void GetSomething(T a)
{
do something
}

public void GetSomething(T a,K b)
{
do something
}


这篇关于多态和泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆