首頁  >  文章  >  後端開發  >  詳細介紹C#中的常用方法

詳細介紹C#中的常用方法

Y2J
Y2J原創
2017-04-21 14:15:141368瀏覽

本篇文章介紹了,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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn