详细介绍C#中的常用方法

Y2J
发布: 2017-04-21 14:15:14
原创
1516人浏览过

本篇文章介绍了,c#中方法的详细说明。需要的朋友参考下

1.让方法返回多个参数

1.1在方法体外定义变量保存结果

 代码如下:

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 pide(int x, int y)
         {
             quotient = x / y;
             remainder = x % y;
         }
         static void Main(string[] args)
         {
             Program.pide(6,9);
             Console.WriteLine(Program.quotient);
             Console.WriteLine(Program.remainder);
             Console.ReadKey();
         }
     }
 }
登录后复制

1.2使用输出型和输入型参数

代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;
namespace Method{
    class Program
    {
       public static void pide(int x, int y, out int quotient, out int remainder) 
       {
            quotient = x / y;
            remainder = x % y;
        }
        static void Main(string[] args)
        {
            int quotient, remainder;
            pide(6,9,out quotient,out remainder);
            Console.WriteLine("{0} {1}",quotient,remainder);
            Console.ReadKey();
        }
    }
}
登录后复制

2.方法的重载

方法重载是面向对象对结构化编程特性的一个重要扩充

构成重载的方法具有以下特点:

(1)方法名相同

(2)方法参数列表不同

判断上述第二点的标准有三点,满足任一点均可认定方法参数列表不同:

(1)方法参数数目不同:

(2)方法拥有相同数目的参数,但参数的类型不一样。

(3)方法拥有相同数目的参数和参数类型,但是参数类型出现的先后顺序不一样,

需要注意的是:方法返回值类型不是方法重载的判断条件。

3.方法的隐藏

代码如下:

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("子类方法");
         }
     }
 }
登录后复制

代码如下:

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("子类方法");
        }
    }
}
登录后复制


在未指明成员存储权限的前提下,其中的成员都是私有的。

代码如下:

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("子类方法");
         }
     }
 }
登录后复制

4.方法重写和虚方法的调用

 代码如下:

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("子类方法");
         }
     }
 }
登录后复制

以上就是详细介绍C#中的常用方法的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
c#
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号