当前位置: 移动技术网 > IT编程>开发语言>c# > C#中方法的详细介绍

C#中方法的详细介绍

2019年07月18日  | 移动技术网IT编程  | 我要评论
1.让方法返回多个参数 1.1在方法体外定义变量保存结果复制代码 代码如下:using system; using system.collections.gen

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 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();

         }
     }
 }

1.2使用输出型和输入型参数
复制代码 代码如下:

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();
        }
    }
}


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("子类方法");
         }
     }
 }

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网