在静态多态性中,对函数的响应是在编译时确定的。在动态多态性中,它是在运行时确定的。动态多态性就是我们所说的后期绑定。动态多态性是通过抽象类和虚函数实现的。以下是一个显示动态多态性示例的示例 −示例 在线演示using System; namespace PolymorphismApplication { class Shape { protected int width, height; public Shape( int a = 0, int b = 0) { width = a; height = b; ... 阅读更多
C# 中的构造函数在创建对象时会自动调用。构造函数的名称与类的名称相同,例如 −public class Department { public Department () { Console.WriteLine("Default Constructor! "); } }以下是显示如何在 C# 中使用默认构造函数的代码。构造函数在创建对象时立即调用 −Department dept1 = new Department ();默认构造函数是没有参数的构造函数,例如 −Department () { }让我们来看一个完整的示例,学习如何使用默认构造函数 −示例 在线…阅读更多
如果在组合中删除父对象,则子对象也会失去其状态。组合是一种特殊的聚合,表示部分与整体的关系。例如,汽车有发动机。如果汽车被破坏,发动机也会被破坏。public class Engine { . . . } public class Car { Engine eng = new Engine(); ....... }
三元运算符是 C# 中的条件运算符。它接受三个参数并计算布尔表达式。例如 −y = (x == 1) ? 70 : 100;上面,如果第一个操作数计算结果为真 (1),则计算第二个操作数。如果第一个操作数计算结果为假 (0),则计算第三个操作数。以下是一个示例 −示例 在线演示using System; namespace DEMO { class Program { static void Main(string[] args) { int a, b; a = 10; b = (a == 1) ? 20 : 30; Console.WriteLine("Value of b is {0}", b); b = (a == 10) ? 20 : 30; Console.WriteLine("Value of b is {0}", b); Console.ReadLine(); } } }输出Value of b is 30 Value of b is 20
C# 程序的主要组成部分包括 −命名空间声明类类方法类属性主方法语句和表达式注释以下是一个显示如何创建 C# 程序的示例 −示例 在线演示using System; namespace Demo { class Program { static void Main(string[] args) { Console.WriteLine("Our first program in C#!"); Console.ReadKey(); } } }输出Our first program in C#!以下是我们上面看到的 C# 程序的各个部分 −using System; - using 关键字用于在程序中包含 System 命名空间。一个程序…阅读更多
具有相同名称但参数不同的两个或多个方法就是我们所说的 C# 中的方法重载。C# 中的方法重载可以通过更改参数的数量和参数的数据类型来执行。假设您有一个打印数字乘积的函数,那么我们的重载方法将具有相同的名称,但参数数量不同 −public static int mulDisplay(int one, int two) { } public static int mulDisplay(int one, int two, int three) { } public static int mulDisplay(int one, int two, int three, int four) { }以下是…阅读更多