中国能源建设招标网站,工具网站有哪些,搭理彩票网站开发,西安建设工程信息网的地址本文由 简悦 SimpRead 转码#xff0c; 原文地址 mp.weixin.qq.com C#面向对象(OOPs)中的多态性
概述#xff1a;在编程语言和类型理论中#xff0c;多态性是为不同类型的实体提供单个接口#xff0c;或者使用单个符号来表示多个不同的类型。多态对象是能够呈现多种形式的… 本文由 简悦 SimpRead 转码 原文地址 mp.weixin.qq.com C#面向对象(OOPs)中的多态性
概述在编程语言和类型理论中多态性是为不同类型的实体提供单个接口或者使用单个符号来表示多个不同的类型。多态对象是能够呈现多种形式的对象。
编译时多态性静态绑定或早期绑定或临时多态性
编译时多态性存在两种类型一种是运算符重载另一种是方法重载
** 方法重载 **
允许一个类具有多个具有相同名称但参数不同的方法。这里只有参数的区别不返回方法的类型。
using System; public class Calculator
{ public int Add(int a, int b) { return a b; } // Overloaded method with different parameter types public double Add(double a, double b) { return a b; } // this is not possible cant have same paramter but different return type public int Add(double a, double b) { return (int) a b; }
} class Program
{ static void Main(string[] args) { Calculator calc new Calculator(); int result1 calc.Add(10, 20); double result2 calc.Add(3.5, 4.5); Console.WriteLine(Result 1: result1); Console.WriteLine(Result 2: result2); }
}** 运算符重载**
允许重新定义运算符以使用用户定义的类型。就像您可以在字符串类型中添加数字一样。
using System; public class Complex
{ public int Real { get; set; } public int Imaginary { get; set; } public Complex(int real 0, int imaginary 0) { Real real; Imaginary imaginary; } // Overloading the operator for adding two Complex objects public static Complex operator (Complex c1, Complex c2) { return new Complex(c1.Real c2.Real, c1.Imaginary c2.Imaginary); }// Overloading the ! operator for adding two Complex objects public static bool operator !(Complex first, Complex second) { return !(first.Real second.Real first.Imaginary second.Imaginary); } // Overloading the operator for adding two Complex objects public static bool operator (Complex first, Complex second) { return first.Real second.Real first.Imaginary second.Imaginary; }public override bool Equals(object? obj){if(obj null || GetType() ! obj.GetType())return false;Complex other (Complex)obj;return Real other.Real Imaginary other.Imaginary;}public override int GetHashCode(){return HashCode.Combine(Real, Imaginary);}public void Display() { Console.WriteLine($Real: {Real}, Imaginary: {Imaginary}); }
} class Program
{ static void Main(string[] args) { Complex c1 new Complex(3, 4); Complex c2 new Complex(5, 6); Complex result c1 c2; // Using the overloaded operator result.Display(); }
}在上面的例子中复杂类重载 和 运算符使用运算符重载您可以提供自定义的自然运算符重载 -、、* 或 等。你在编译时定义的所有东西这就是为什么这被称为编译时重载。默认情况下在 c# 中整数和字符串之间存在默认的 运算符重载。
运行时多态性动态绑定或后期绑定
** 方法覆盖** :
当子类提供已在其超类或父类中定义的方法的特定实现时发生。子类或子类方法重写具有相同签名和相同返回类型的超类方法。
using System; // Base class
class Shape
{ public virtual void Draw() { Console.WriteLine(Drawing a shape); }
} // Derived class
class Circle : Shape
{ public override void Draw() { Console.WriteLine(Drawing a circle); }
} class Program
{ static void Main(string[] args) { // Runtime binding example Shape shape new Circle(); // Creating instance of derived class but of base class reference shape.Draw(); // This will call the overridden method in Circle class at runtime }
}方法覆盖 在此示例中该类定义了一个虚拟方法 ShapeDraw() 。 该类继承自该方法并使用其自己的实现重写该方法 CircleShapeDraw() 。 当子类提供已在其超类 中定义的方法的特定实现时将发生方法重写。 Shape中的方法使用相同的签名和返回类型重写 in 中的方法。Draw()CircleDraw()Shape
** 运行时绑定** 在该方法中创建一个实例 并将其分配给 类型的引用。MainCircleShape 这是多态性 **子类型多态性的一个例子后面的 i 节将对此进行解释 **其中派生类 对象可以被视为其基类 类型。CircleShape 在运行时当调用时C# 运行时系统将确定对象 的实际类型并执行 中重写的方法。shape.Draw()CircleCircle 这称为运行时绑定或后期绑定其中在运行时根据对象的实际类型决定调用哪个方法。
** 参数多态性泛型编程**
** 泛 型** 允许编写函数或类这些函数或类可以在编译时对各种数据类型进行操作而无需知道特定类型。它通过对类型进行抽象来提供灵活性和代码可重用性。
using System; public class StackT
{ private T[] elements; private int top; public Stack(int size) { elements new T[size]; top -1; } public void Push(T item) { if (top elements.Length - 1) { Console.WriteLine(Stack overflow!); return; } elements[top] item; } public T Pop() { if (top -1) { Console.WriteLine(Stack underflow!); return default(T); } return elements[top--]; } public void Print() { Console.WriteLine(Stack elements:); for (int i top; i 0; i--) { Console.WriteLine(elements[i]); } }
} class Program
{ static void Main(string[] args) { Stackint intStack new Stackint(5); intStack.Push(1); intStack.Push(2); intStack.Push(3); intStack.Print(); Console.WriteLine(Popped item: intStack.Pop()); Stackstring stringStack new Stackstring(3); stringStack.Push(Hello); stringStack.Push(World); stringStack.Print(); Console.WriteLine(Popped item: stringStack.Pop()); }
}在此示例中 我们定义了一个使用泛型类型参数的类Stack 该类可以使用创建类实例时指定的任何数据类型。 我们演示了如何使用 int 和 string 类型来使用类。Stack, Stack 通过使用泛型该类允许相同的实现使用不同的数据类型从而提供灵活性和代码可重用性。
** 亚型多态性Inclusion Polymorphism**
方法覆盖 使派生类的对象能够被视为其基类的对象从而允许多态行为。当派生类继承自基类并提供其自己的某些方法的实现时就会发生这种情况从而重写基类方法的行为。
using System; // Interface
public interface IShape
{ void Draw();
} // Class implementing the interface
public class Circle : IShape
{ public void Draw() { Console.WriteLine(Drawing a circle); }
} // Another class implementing the interface
public class Rectangle : IShape
{ public void Draw() { Console.WriteLine(Drawing a rectangle); }
} class Program
{ static void Main(string[] args) { IShape shape1 new Circle(); IShape shape2 new Rectangle(); shape1.Draw(); // Calls the Draw method of Circle shape2.Draw(); // Calls the Draw method of Rectangle }
}类Circle和类RectangleI都实现接口并提供自己的ShapeDraw方法实现。 在运行时我们创建 Circle 和 Rectangle 类的对象并将它们分配给 IShape 类型的变量。
大多数设计模式使用子类型多态来实现灵活和鲁棒的类设计这是一种非常强大的多态行为如策略设计模式或观察者设计模式或装饰器设计模式等。
** 强制多态性铸造**
强制多态性也称为隐式转换是指编译器或运行时环境将一种数据类型自动转换为另一种数据类型。这种转换是在安全且合乎逻辑的情况下进行的从而允许更灵活和方便的编程。
using System; class Program
{ static void Main(string[] args) { int num1 10; double num2 num1; // Implicit conversion from int to double Console.WriteLine(num1: num1); // Outputs: num1: 10 Console.WriteLine(num2: num2); // Outputs: num2: 10.0 }
}在此示例中 我们有一个初始化的整数变量num1值为 10。 我们将 num1的值赋值给一个双精度变量num2。此赋值将触发从 int到 double的隐式转换。 编译器会自动将int值转换为double值因为这样做是安全的不会损失精度。 这种隐式转换允许我们将num2视为一个double即使它最初是一个int** 表明强制多态性。**
总结
面向对象编程中的多态性包括编译时和运行时行为。编译时多态性涉及方法和运算符重载允许基于参数和运算符的多种行为。
通过方法重写实现的运行时多态性使子类能够提供自己的超类方法实现。其他形式包括具有泛型的参数多态性、通过方法覆盖和接口的子类型多态性以及用于隐式类型转换的强制多态性。这些概念有助于提高软件开发中的代码灵活性、可重用性和适应性。 文章转载自: http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.mxhys.cn.gov.cn.mxhys.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.mlyq.cn.gov.cn.mlyq.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.gygfx.cn.gov.cn.gygfx.cn http://www.morning.kpypy.cn.gov.cn.kpypy.cn http://www.morning.zcfmb.cn.gov.cn.zcfmb.cn http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.rnqrl.cn.gov.cn.rnqrl.cn http://www.morning.tckxl.cn.gov.cn.tckxl.cn http://www.morning.pzbjy.cn.gov.cn.pzbjy.cn http://www.morning.tdxlj.cn.gov.cn.tdxlj.cn http://www.morning.stbfy.cn.gov.cn.stbfy.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.kynf.cn.gov.cn.kynf.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.gqfbl.cn.gov.cn.gqfbl.cn http://www.morning.wjfzp.cn.gov.cn.wjfzp.cn http://www.morning.bwhcl.cn.gov.cn.bwhcl.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.mjpgl.cn.gov.cn.mjpgl.cn http://www.morning.sbkb.cn.gov.cn.sbkb.cn http://www.morning.osshjj.cn.gov.cn.osshjj.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.clccg.cn.gov.cn.clccg.cn http://www.morning.nkmw.cn.gov.cn.nkmw.cn http://www.morning.pmysp.cn.gov.cn.pmysp.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.kqgqy.cn.gov.cn.kqgqy.cn http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.rqlf.cn.gov.cn.rqlf.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn http://www.morning.skbhl.cn.gov.cn.skbhl.cn http://www.morning.zqcdl.cn.gov.cn.zqcdl.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.lfpzs.cn.gov.cn.lfpzs.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.nbsfb.cn.gov.cn.nbsfb.cn http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.kqgsn.cn.gov.cn.kqgsn.cn http://www.morning.tnktt.cn.gov.cn.tnktt.cn http://www.morning.pqqzd.cn.gov.cn.pqqzd.cn http://www.morning.kchwr.cn.gov.cn.kchwr.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.qrcsb.cn.gov.cn.qrcsb.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.zsleyuan.cn.gov.cn.zsleyuan.cn http://www.morning.lnbcx.cn.gov.cn.lnbcx.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.rbgwj.cn.gov.cn.rbgwj.cn http://www.morning.txqsm.cn.gov.cn.txqsm.cn http://www.morning.lcwhn.cn.gov.cn.lcwhn.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.qysnd.cn.gov.cn.qysnd.cn http://www.morning.bpmfq.cn.gov.cn.bpmfq.cn http://www.morning.nxwk.cn.gov.cn.nxwk.cn http://www.morning.kbgzj.cn.gov.cn.kbgzj.cn