国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中方法的詳細介紹

2020-01-24 03:25:04
字體:
來源:轉載
供稿:網友

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 马尔康县| 怀来县| 东光县| 防城港市| 平阳县| 曲松县| 宿州市| 宁化县| 九寨沟县| 淮安市| 台南市| 萍乡市| 大同市| 上高县| 达孜县| 高清| 漳平市| 武城县| 泗阳县| 东乌珠穆沁旗| 拉萨市| 将乐县| 石嘴山市| 德钦县| 拜泉县| 交城县| 安阳县| 华坪县| 临猗县| 绥芬河市| 上杭县| 山东| 贞丰县| 云龙县| 平定县| 葫芦岛市| 哈密市| 长治县| 集贤县| 郁南县| 镇康县|