最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中方法的詳細(xì)介紹

 更新時(shí)間:2013年04月26日 17:30:21   作者:  
本篇文章介紹了,C#中方法的詳細(xì)說(shuō)明。需要的朋友參考下

1.讓方法返回多個(gè)參數(shù)

1.1在方法體外定義變量保存結(jié)果

復(fù)制代碼 代碼如下:

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使用輸出型和輸入型參數(shù)
復(fù)制代碼 代碼如下:

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.方法的重載

方法重載是面向?qū)ο髮?duì)結(jié)構(gòu)化編程特性的一個(gè)重要擴(kuò)充

構(gòu)成重載的方法具有以下特點(diǎn):

(1)方法名相同

(2)方法參數(shù)列表不同

判斷上述第二點(diǎn)的標(biāo)準(zhǔn)有三點(diǎn),滿足任一點(diǎn)均可認(rèn)定方法參數(shù)列表不同:

(1)方法參數(shù)數(shù)目不同:

(2)方法擁有相同數(shù)目的參數(shù),但參數(shù)的類型不一樣。

(3)方法擁有相同數(shù)目的參數(shù)和參數(shù)類型,但是參數(shù)類型出現(xiàn)的先后順序不一樣,

需要注意的是:方法返回值類型不是方法重載的判斷條件。

3.方法的隱藏

復(fù)制代碼 代碼如下:

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

復(fù)制代碼 代碼如下:

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

在未指明成員存儲(chǔ)權(quán)限的前提下,其中的成員都是私有的。
復(fù)制代碼 代碼如下:

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.方法重寫和虛方法的調(diào)用
復(fù)制代碼 代碼如下:

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

相關(guān)文章

最新評(píng)論

招远市| 普洱| 玛多县| 深圳市| 苗栗市| 新昌县| 鄂托克前旗| 静海县| 柞水县| 湘西| 潞城市| 卢龙县| 藁城市| 宾阳县| 花垣县| 沁水县| 寿阳县| 名山县| 临安市| 任丘市| 中宁县| 凌云县| 邢台县| 方城县| 阜康市| 青阳县| 抚顺市| 河池市| 体育| 江津市| 湖北省| 白朗县| 长葛市| 比如县| 古交市| 聊城市| 乌鲁木齐县| 高碑店市| 陆川县| 南充市| 长乐市|