早教吧作业答案频道 -->其他-->
c#什么是多态性,用多态的作用是什么?请举例说明,
题目详情
c#什么是多态性,用多态的作用是什么?请举例说明,
▼优质解答
答案和解析
多态的含义就是C#中允许多个方法的方法名相同,只要它们的方法签名不同就可以.
这里有两个概念,方法名是方法的一部分,例如一个方法:
public static void hello(int a,int b)
在这个方法中,hello被称为方法名.
方法签名指的是方法名和方法参数列表的构造,同样对于上面的方法,它的方法签名是:
hello(int a,int b)
多态性的作用是极大的,您可能已经知道,下面的代码是可以通过编译的:
using System;
namespace Test
{
public class MainClass
{
public static void Main()
{
Console.WriteLine("hahaha");
Console.WriteLine(5);
Console.WriteLine(true);
}
}
}
这段代码的输出是:
hahaha
5
true
C#是一种强类型语言,它的不同类型必须经过转换才能进行处理,那么为什么一个WriteLine()方法既可以输出字符串类型的常量,又可以输出整型和布尔型的常量呢?原因就在于多态性.
下面是一个例子,我写了一个类Test,它包含一系列方法,方法名都是Print,我对不同的输入参数进行了处理,没有使用WriteLine方法的重载.
//test.cs
//可以使用 csc test.cs 编译这段代码或复制到VC#中.
using System;
namespace TestFunc
{
public class Test
{
public static void Print(string str)
{
Console.WriteLine(str);
}
public static void Print(int i)
{
Console.WriteLine(i.ToString());//调用ToString方法把整型转换为string类型.
}
public static void Print(bool b)
{
if (b == true)//判断后输出结果.
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
}
public static void Print(params string[] str)
{
//这个方法实现了对未知数量的参数的输出.使用params关键字.
for (int i = 0; i < str.Length; ++i)
{
Console.WriteLine(str[i]);
}
}
}
public class MainClass
{
public static void Main()
{
bool a = false;
Test.Print("david","jack","mike");
Test.Print(5);
Test.Print(true);
Test.Print(a);
Test.Print("successful!");
}
}
}
程序执行的输出是:
david
jack
mike
5
True
False
successful!
请注意程序中有注释的部分,这样,我只需要一个方法就可以以安全的方法处理各种的数据.
这里有两个概念,方法名是方法的一部分,例如一个方法:
public static void hello(int a,int b)
在这个方法中,hello被称为方法名.
方法签名指的是方法名和方法参数列表的构造,同样对于上面的方法,它的方法签名是:
hello(int a,int b)
多态性的作用是极大的,您可能已经知道,下面的代码是可以通过编译的:
using System;
namespace Test
{
public class MainClass
{
public static void Main()
{
Console.WriteLine("hahaha");
Console.WriteLine(5);
Console.WriteLine(true);
}
}
}
这段代码的输出是:
hahaha
5
true
C#是一种强类型语言,它的不同类型必须经过转换才能进行处理,那么为什么一个WriteLine()方法既可以输出字符串类型的常量,又可以输出整型和布尔型的常量呢?原因就在于多态性.
下面是一个例子,我写了一个类Test,它包含一系列方法,方法名都是Print,我对不同的输入参数进行了处理,没有使用WriteLine方法的重载.
//test.cs
//可以使用 csc test.cs 编译这段代码或复制到VC#中.
using System;
namespace TestFunc
{
public class Test
{
public static void Print(string str)
{
Console.WriteLine(str);
}
public static void Print(int i)
{
Console.WriteLine(i.ToString());//调用ToString方法把整型转换为string类型.
}
public static void Print(bool b)
{
if (b == true)//判断后输出结果.
{
Console.WriteLine("True");
}
else
{
Console.WriteLine("False");
}
}
public static void Print(params string[] str)
{
//这个方法实现了对未知数量的参数的输出.使用params关键字.
for (int i = 0; i < str.Length; ++i)
{
Console.WriteLine(str[i]);
}
}
}
public class MainClass
{
public static void Main()
{
bool a = false;
Test.Print("david","jack","mike");
Test.Print(5);
Test.Print(true);
Test.Print(a);
Test.Print("successful!");
}
}
}
程序执行的输出是:
david
jack
mike
5
True
False
successful!
请注意程序中有注释的部分,这样,我只需要一个方法就可以以安全的方法处理各种的数据.
看了 c#什么是多态性,用多态的作...的网友还看了以下:
长跑前能否吃巧克力?我明天下午3:00要1000米长跑问:能不能吃巧克力,网上有的说能吃,有的说不能 2020-03-30 …
人们有时要利用惯性,有时要防止惯性带来的危害,请你就以上两点各举出一个例子.利用:防止:最好举最用 2020-04-05 …
用说明性的语言概述画面的内容 2020-05-13 …
中国传统年画,常常借助于喜庆明丽的色彩和象征吉祥的事物来表现美好的愿望。如鱼象征着年年有余,凤凰象 2020-05-13 …
用说明性文章来介绍电器,400字以上 2020-05-15 …
如果好久没有跟老外联系了,写信的开头应该怎么说啊?或者就不用说什么的.中文一般是好久没跟你写信了就 2020-06-13 …
综合性学习请用说明性的语言介绍一种你最喜欢的植物的特征及其功能。要求:(1)至少用上两种说明方法。( 2020-11-15 …
作文请用说明性的语言介绍你最喜爱的一件工艺品,要求抓住说明对象的特点,运用适当的说明方法,语言通顺。 2020-11-24 …
观察下面这幅漫画,回答问题。爷爷,许个愿吧!(1)用说明性的语言介绍这幅漫画的内容。(2)漫画中的老 2020-11-28 …
中国传统年画,常借助象征吉祥的事物来表达美好的愿望,如鱼象征着年年有余,凤凰象征富贵吉祥等。请用说明 2020-12-09 …