源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

C#中方法的详细介绍

  • 时间:2021-02-21 02:02 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:C#中方法的详细介绍
[b]1.让方法返回多个参数[/b] [b]1.1在方法体外定义变量保存结果 [/b]
[u]复制代码[/u] 代码如下:
using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;  namespace Method  {      class Program      {          public static int quotient;          public static int remainder;          public static void Divide(int x, int y)          {              quotient = x / y;              remainder = x % y;          }          static void Main(string[] args)          {              Program.Divide(6,9);              Console.WriteLine(Program.quotient);              Console.WriteLine(Program.remainder);              Console.ReadKey();          }      }  }
[b]1.2使用输出型和输入型参数 [/b]
[u]复制代码[/u] 代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Method {     class Program     {         public static void Divide(int x, int y, out int quotient, out int remainder)         {             quotient = x / y;             remainder = x % y;         }         static void Main(string[] args)         {             int quotient, remainder;             Divide(6,9,out quotient,out remainder);             Console.WriteLine("{0} {1}",quotient,remainder);             Console.ReadKey();         }     } }
[b]2.方法的重载[/b] 方法重载是面向对象对结构化编程特性的一个重要扩充 构成重载的方法具有以下特点: (1)方法名相同 (2)方法参数列表不同 判断上述第二点的标准有三点,满足任一点均可认定方法参数列表不同: (1)方法参数数目不同: (2)方法拥有相同数目的参数,但参数的类型不一样。 (3)方法拥有相同数目的参数和参数类型,但是参数类型出现的先后顺序不一样, 需要注意的是:方法返回值类型不是方法重载的判断条件。 [b]3.方法的隐藏 [/b]
[u]复制代码[/u] 代码如下:
namespace 方法隐藏  {      class Program      {          static void Main(string[] args)          {              Parent p = new Child();              p.show();              Console.ReadKey();          }      }      class Parent      {          public void show()          {              Console.Write("父类方法");          }      }      class Child : Parent      {          public new void show()          {              Console.Write("子类方法");          }      }  }
[u]复制代码[/u] 代码如下:
namespace 方法隐藏 {     class Program     {         static void Main(string[] args)         {             Parent.show();             Console.ReadKey();             Child.show();//父类方法         }     }     class Parent     {         public static void show()         {             Console.Write("父类方法");         }     }     class Child : Parent     {         public static new void show()         {             Console.Write("子类方法");         }     } }
[b]在未指明成员存储权限的前提下,其中的成员都是私有的。 [/b]
[u]复制代码[/u] 代码如下:
namespace 方法隐藏  {      class Program      {          static void Main(string[] args)          {              Parent p1= new Parent();              Parent p2 = new Child();              p1.show();//父类方法              p2.show();//父类方法              ((Child)p2).show();//父类方法              Console.ReadKey();          }      }      class Parent      {          public  void show()          {              Console.WriteLine("父类方法");          }      }      class Child : Parent      {           new void show()          {              Console.WriteLine("子类方法");          }      }  }
[b]4.方法重写和虚方法的调用 [/b]
[u]复制代码[/u] 代码如下:
namespace 方法重写  {      class Program      {          static void Main(string[] args)          {              Parent p1 = new Parent();              Parent p2 = new Child();              p1.show();              p2.show();              ((Parent)p2).show();//子类方法              Console.ReadKey();          }      }      class Parent      {          public virtual void show()          {              Console.WriteLine("父类方法");          }      }      class Child:Parent      {          public override void show()          {              Console.WriteLine("子类方法");          }      }  }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部