C# Polymorphism(C# 多态性)
问题描述
运行时多态性和编译时多态性有什么区别?另外,早期绑定和后期绑定有什么区别?示例将不胜感激.
What's the difference between run-time polymorphism and compile-time polymorphism? Also, what's the difference between early binding and late binding? Examples would be highly appreciated.
推荐答案
编译时多态
方法重载就是一个很好的例子.您可以有两个具有相同名称但具有不同签名的方法.编译器会在编译时选择正确的版本.
Method overloading is a great example. You can have two methods with the same name but with different signatures. The compiler will choose the correct version to use at compile time.
运行时多态性
在子类中覆盖父类的虚方法就是一个很好的例子.另一个是从接口实现方法的类.这允许您在使用子指定的实现时在代码中使用更通用的类型.给定以下类定义:
Overriding a virtual method from a parent class in a child class is a good example. Another is a class implementing methods from an Interface. This allows you to use the more generic type in code while using the implementation specified by the child. Given the following class definitions:
public class Parent
{
public virtual void SayHello() { Console.WriteLine("Hello World!"); }
}
public class Child : Parent
{
public override void SayHello() { Console.WriteLine("Goodbye World!"); }
}
以下代码将输出再见世界!":
The following code will output "Goodbye World!":
Parent instance = new Child();
instance.SayHello();
早期绑定
在编译时指定类型:
SqlConnection conn = new SqlConnection();
后期装订
类型在运行时确定:
object conn = Activator.CreateInstance("System.Data.SqlClient.SqlConnection");
这篇关于C# 多态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# 多态性
基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
