• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 13, 文章 - 0, 評(píng)論 - 3, 引用 - 0
            數(shù)據(jù)加載中……

            C#中的繼承與覆蓋

            C#中繼承與函數(shù)覆蓋,有時(shí)比較復(fù)雜,不小心很容易弄錯(cuò)。所以有必要總結(jié)點(diǎn)規(guī)律出來(lái)。
            (1) 當(dāng)子類(lèi)中擁有和父類(lèi)一樣的可見(jiàn)field和member function時(shí),子類(lèi)中的field和member function會(huì)覆蓋父類(lèi)中的定義。
            using System;

            namespace
             TestInheritance

                
            public class
             Bird 
                
            {
                    
            public string type = "Bird"
            ;

                    
                    
            public void
             ShowType()
                    
            {
                        Console.WriteLine(
            "Type is {0}"
            , type);
                    }

               
             }

                
            public class Chicken : Bird
                
            {
                    
            //同名的field,可見(jiàn)性為public或者protected,不管有沒(méi)有聲明為new,均會(huì)覆蓋父類(lèi)中的field

                   public new string type = "Chicken";
                  
             
                    
            //當(dāng)子類(lèi)含有和父類(lèi)相同的函數(shù)簽名(可見(jiàn)性public或者protected、函數(shù)名和參數(shù)都相同)時(shí),不管帶new與否,都將覆蓋父類(lèi)中相同的函數(shù)

                    public /*new*/ void ShowType()
                    
            { Console.WriteLine();                     
                        Console.WriteLine(
            "This is Chicken::ShowType()"
            , type);
                    }
                 
              
             }


                
            public class TestInheritance
                
            {     

                    
            public static void
             Main()
                    
            {
                        Bird bird 
            = new
             Chicken();
                        bird.ShowType();
                       
            Console.WriteLine();   
                        Chicken chicken = new
             Chicken();
                        chicken.ShowType();
                        Console.WriteLine();
                        Console.WriteLine(
            "Type is {0}"
            , chicken.type);
                        Console.Read();

                    }

                }

            }
            如上例中的輸出會(huì)是:
            Type is Bird

            This is Chicken::ShowType()

            Type is Chicken

            (2) 當(dāng)子類(lèi)中的member function 和field作用范圍比父類(lèi)中對(duì)應(yīng)的member function和field小時(shí),只會(huì)對(duì)作用范圍內(nèi)的部分產(chǎn)生覆蓋影響,對(duì)作用范圍外無(wú)影響。
            如下例:
            using System;

            namespace TestInheritance

                
            public class Bird 
                
            {
                    
            public string type = "Bird";

                    
                    
            public void ShowType()
                    
            {
                        Console.WriteLine(
            "Type is {0}", type);
                    }

                   
                }

                
            public class Chicken : Bird
                
            {
                    
            //這里的type只會(huì)對(duì)Chicken內(nèi)部的函數(shù)有影響,出了Chicken類(lèi),該函數(shù)不具有作用。
                    private new string type = "Chicken";

                    
            //這里的ShowType()函數(shù)只會(huì)對(duì)Chicken內(nèi)部的函數(shù)有影響,出了Chicken類(lèi),該函數(shù)不具有作用。
                    private /*new*/ void ShowType()
                    
            {
                        Console.WriteLine(
            "This is Chicken::ShowType()", type);
                    }

                 
                    
            public void ShowInfo()
                    
            {
                        ShowType();           
                    }

                }


                
            public class TestInheritance
                
            {     

                    
            public static void Main()
                    
            {
                        Bird bird 
            = new Chicken();
                        bird.ShowType();
                        Console.WriteLine();

                        Chicken chicken = new Chicken();
                        chicken.ShowType();
                        Console.WriteLine();
                        Console.WriteLine(
            "Type is {0}", chicken.type);
                        Console.WriteLine();
                        chicken.ShowInfo();
                        Console.Read();

                    }

                }

            }
            輸出:
            Type is Bird

            This is Bird

            Type is Bird

            This is Chicken::ShowType()

            又如下例:
            using System;

            namespace TestInheritance

                
            public class Bird 
                
            {
                    
            public string type = "Bird";

                    
                    
            public void ShowType()
                    
            {
                        Console.WriteLine(
            "Type is {0}", type);
                    }

                   
                }

                
            public class Chicken : Bird
                
            {
                    
            //這里的type只會(huì)對(duì)Chicken內(nèi)部的函數(shù)有影響,出了Chicken類(lèi),該函數(shù)不具有作用。
                    protected new string type = "Chicken";

                    
            //這里的ShowType()函數(shù)只會(huì)對(duì)Chicken內(nèi)部的函數(shù)有影響,出了Chicken類(lèi),該函數(shù)不具有作用。
                    protected /*new*/ void ShowType()
                    
            {
                        Console.WriteLine(
            "This is Chicken::ShowType()", type);
                    }

                 
                    
            public void ShowInfo()
                    
            {
                        ShowType();           
                    }

                }


               
                
            public class Cock : Chicken
                
            {
                    
            public void ShowInfo()
                    
            {
                        Console.WriteLine(
            "Cock::ShowInfo()");
                        
            //這里會(huì)調(diào)用Chicken的type成員
                        Console.WriteLine(type);
                        
            //這里將調(diào)用Chicken的ShowType()函數(shù)
                        ShowType();
                    }

                }


                
            public class TestInheritance
                
            {     

                    
            public static void Main()
                    
            {
                        Bird bird 
            = new Chicken();
                        
            //調(diào)用Bird的ShowType()函數(shù)
                        bird.ShowType();
                        
                        Console.WriteLine();
                        Chicken chicken 
            = new Chicken();

                        
            //這里會(huì)調(diào)用Bird的ShowType();
                        chicken.ShowType();
                        
            //這里chicken.type會(huì)為Bird,因?yàn)镃hicken類(lèi)中type為protected,在這里沒(méi)法訪問(wèn)
                        Console.WriteLine("Type is {0}", chicken.type);
                        chicken.ShowInfo();      
                 
                        Console.WriteLine();
                        Cock cock 
            = new Cock();
                        cock.ShowInfo();
                        Console.Read();
                    }

                }

            }
            會(huì)輸出:
            Type is Bird

            Type is Bird
            Type is Bird
            This is Chicken::ShowType()

            Cock::ShowInfo()
            Chicken
            This is Chicken::ShowType()

            (3) 當(dāng)子類(lèi)中的member function 和父類(lèi)的member function名字相同但參數(shù)不同時(shí),不會(huì)產(chǎn)生覆蓋。如:
            using System;

            namespace TestInheritance

                
            public class Bird 
                
            {
                    
            public string type = "Bird";

                    
                    
            public void ShowType()
                    
            {
                        Console.WriteLine(
            "Type is {0}", type);
                    }

                   
                }

                
            public class Chicken : Bird
                
            {
                    
            //這里的type只會(huì)對(duì)Chicken內(nèi)部的函數(shù)有影響,出了Chicken類(lèi),該函數(shù)不具有作用。
                    protected new string type = "Chicken";

                    
            //不會(huì)覆蓋Bird的ShowType函數(shù),因?yàn)楹瘮?shù)簽名不同(參數(shù)不同)
                    public /*new*/ void ShowType(int x)
                    
            {
                        Console.WriteLine(
            "int {0}",x);
                        Console.WriteLine(
            "This is Chicken::ShowType()", type);
                    }

                 
                    
            public void ShowInfo()
                    
            {
                        
            //會(huì)調(diào)用Bird的ShowType函數(shù)
                        Console.WriteLine();
                        Console.WriteLine(
            "Chicken::ShowInfo()");
                        ShowType();           
                    }

                }


               
                
            public class Cock : Chicken
                
            {
                    
            public void ShowInfo()
                    
            {
                        Console.WriteLine();
                        Console.WriteLine(
            "Cock::ShowInfo()");
                        
            //這里會(huì)調(diào)用Chicken的type成員
                        Console.WriteLine(type);
                        
            //這里將調(diào)用Bird的ShowType()函數(shù)
                        ShowType();
                    }

                }


                
            public class TestInheritance
                
            {     

                    
            public static void Main()
                    
            {
                        Bird bird 
            = new Chicken();
                        
            //調(diào)用Bird的ShowType()函數(shù)
                        bird.ShowType();
                      
                        Chicken chicken 
            = new Chicken();

                        
            //這里會(huì)調(diào)用Bird的ShowType();
                        chicken.ShowType();
                        
            //這里chicken.type會(huì)為Bird,因?yàn)镃hicken類(lèi)中type為protected,在這里沒(méi)法訪問(wèn)
                        Console.WriteLine("Type is {0}", chicken.type);
                        chicken.ShowInfo();           

                        Cock cock 
            = new Cock();
                        cock.ShowInfo();
                        Console.Read();
                    }

                }

            }

            輸出為:
            Type is Bird
            This is Bird
            Type is Bird

            Chicken::ShowInfo()
            Type is Bird

            Cock::ShowInfo()
            Chicken
            Type is Bird。

            這里沒(méi)有列舉虛函數(shù)和override,考慮到篇幅,那個(gè)將在其它文章中予以說(shuō)明,相信通過(guò)以上幾個(gè)實(shí)例,大家可以對(duì)函數(shù)覆蓋有個(gè)比較好的了解。

            posted on 2009-04-12 01:02 五味雜陳 閱讀(1768) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): .NET

            久久久久无码国产精品不卡| 99久久精品免费看国产一区二区三区 | 亚洲精品乱码久久久久久自慰| 国产亚洲精品美女久久久| 久久se精品一区二区影院 | 久久久一本精品99久久精品88| 久久精品国产欧美日韩| 国产精品一区二区久久国产| 久久久久se色偷偷亚洲精品av| 国产精品丝袜久久久久久不卡| 人妻无码中文久久久久专区| 亚洲精品综合久久| 欧美国产成人久久精品| 99久久国产综合精品成人影院| 狼狼综合久久久久综合网| 伊人久久大香线蕉综合热线| 九九热久久免费视频| 亚洲午夜久久影院| 国内精品伊人久久久久av一坑| 久久精品国产精品亚洲精品| 日本加勒比久久精品| 国内精品久久久久久久亚洲| 伊人久久大香线蕉精品| 国产一区二区三区久久| 久久国产亚洲高清观看| 亚洲va中文字幕无码久久| 久久精品国产亚洲AV影院| 伊人久久五月天| 久久久久久精品免费免费自慰| 久久婷婷是五月综合色狠狠| 久久久久亚洲av成人无码电影 | WWW婷婷AV久久久影片| 国产精品久久久久jk制服| 国产A三级久久精品| 国产69精品久久久久9999APGF| 久久久亚洲欧洲日产国码是AV| 国产精品久久新婚兰兰| 久久亚洲AV无码精品色午夜 | 久久免费视频网站| 精品国产福利久久久| 成人精品一区二区久久久|