C#方法

1.让方法返回多个参数

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

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Method
 7 {
 8     class Program
 9     {
10         public static int quotient;
11         public static int remainder;
12         public static void Divide(int x, int y)
13         {
14             quotient = x / y;
15             remainder = x % y;
16         }
17         static void Main(string[] args)
18         {
19             Program.Divide(6,9);
20             Console.WriteLine(Program.quotient);
21             Console.WriteLine(Program.remainder);
22             Console.ReadKey();
23 
24         }
25     }
26 }

 

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.方法的隐藏

 1 namespace 方法隐藏
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Parent p = new Child();
 8             p.show();
 9             Console.ReadKey();
10         }
11     }
12     class Parent
13     {
14         public void show()
15         {
16             Console.Write("父类方法");
17         }
18     }
19     class Child : Parent
20     {
21         public new void show()
22         {
23             Console.Write("子类方法");
24         }
25     }
26 }
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("子类方法");
        }
    }
}

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

 1 namespace 方法隐藏
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Parent p1= new Parent();
 8             Parent p2 = new Child();
 9             p1.show();//父类方法
10             p2.show();//父类方法
11             ((Child)p2).show();//父类方法
12             Console.ReadKey();
13         }
14     }
15     class Parent
16     {
17         public  void show()
18         {
19             Console.WriteLine("父类方法");
20         }
21     }
22     class Child : Parent
23     {
24          new void show()
25         {
26             Console.WriteLine("子类方法");
27         }
28     }
29 }


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

 1 namespace 方法重写
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Parent p1 = new Parent();
 8             Parent p2 = new Child();
 9             p1.show();
10             p2.show();
11             ((Parent)p2).show();//子类方法
12             Console.ReadKey();
13         }
14     }
15     class Parent
16     {
17         public virtual void show()
18         {
19             Console.WriteLine("父类方法");
20         }
21     }
22     class Child:Parent
23     {
24         public override void show()
25         {
26             Console.WriteLine("子类方法");
27         }
28     }
29 }

 

 

posted @ 2012-07-20 23:06  Carve_Time  阅读(183)  评论(0编辑  收藏  举报