• <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, 評論 - 3, 引用 - 0
            數(shù)據(jù)加載中……

            C#中的繼承與覆蓋

            C#中繼承與函數(shù)覆蓋,有時比較復雜,不小心很容易弄錯。所以有必要總結點規(guī)律出來。
            (1) 當子類中擁有和父類一樣的可見field和member function時,子類中的field和member function會覆蓋父類中的定義。
            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,可見性為public或者protected,不管有沒有聲明為new,均會覆蓋父類中的field

                   public new string type = "Chicken";
                  
             
                    
            //當子類含有和父類相同的函數(shù)簽名(可見性public或者protected、函數(shù)名和參數(shù)都相同)時,不管帶new與否,都將覆蓋父類中相同的函數(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();

                    }

                }

            }
            如上例中的輸出會是:
            Type is Bird

            This is Chicken::ShowType()

            Type is Chicken

            (2) 當子類中的member function 和field作用范圍比父類中對應的member function和field小時,只會對作用范圍內(nèi)的部分產(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只會對Chicken內(nèi)部的函數(shù)有影響,出了Chicken類,該函數(shù)不具有作用。
                    private new string type = "Chicken";

                    
            //這里的ShowType()函數(shù)只會對Chicken內(nèi)部的函數(shù)有影響,出了Chicken類,該函數(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只會對Chicken內(nèi)部的函數(shù)有影響,出了Chicken類,該函數(shù)不具有作用。
                    protected new string type = "Chicken";

                    
            //這里的ShowType()函數(shù)只會對Chicken內(nèi)部的函數(shù)有影響,出了Chicken類,該函數(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()");
                        
            //這里會調(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();

                        
            //這里會調(diào)用Bird的ShowType();
                        chicken.ShowType();
                        
            //這里chicken.type會為Bird,因為Chicken類中type為protected,在這里沒法訪問
                        Console.WriteLine("Type is {0}", chicken.type);
                        chicken.ShowInfo();      
                 
                        Console.WriteLine();
                        Cock cock 
            = new Cock();
                        cock.ShowInfo();
                        Console.Read();
                    }

                }

            }
            會輸出:
            Type is Bird

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

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

            (3) 當子類中的member function 和父類的member function名字相同但參數(shù)不同時,不會產(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只會對Chicken內(nèi)部的函數(shù)有影響,出了Chicken類,該函數(shù)不具有作用。
                    protected new string type = "Chicken";

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

                 
                    
            public void ShowInfo()
                    
            {
                        
            //會調(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()");
                        
            //這里會調(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();

                        
            //這里會調(diào)用Bird的ShowType();
                        chicken.ShowType();
                        
            //這里chicken.type會為Bird,因為Chicken類中type為protected,在這里沒法訪問
                        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。

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

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

            久久国产成人午夜AV影院| 性欧美大战久久久久久久| 久久人人妻人人爽人人爽| 久久夜色精品国产噜噜噜亚洲AV| 久久久久久精品成人免费图片 | 国产AⅤ精品一区二区三区久久 | 99精品国产99久久久久久97| 久久久免费精品re6| 久久久精品久久久久特色影视| 久久久久久精品免费看SSS| 波多野结衣中文字幕久久| 亚洲精品午夜国产va久久| 亚洲国产精品无码久久久蜜芽 | 99国产精品久久久久久久成人热| 伊人丁香狠狠色综合久久| 亚洲综合久久久| 久久国产精品免费一区| 国产婷婷成人久久Av免费高清| 久久夜色精品国产噜噜亚洲a| 国产亚洲精品美女久久久| 影音先锋女人AV鲁色资源网久久 | 免费无码国产欧美久久18| 久久久久久a亚洲欧洲aⅴ| 欧洲国产伦久久久久久久| 99久久精品日本一区二区免费 | 日韩人妻无码一区二区三区久久 | 久久青青草原国产精品免费| 天天综合久久一二三区| 亚洲色大成网站www久久九| 久久中文精品无码中文字幕| 久久这里只精品国产99热| 久久精品九九亚洲精品天堂| 亚洲乱码精品久久久久..| 久久99精品国产麻豆婷婷| 99精品久久精品| 亚洲伊人久久精品影院| 亚洲美日韩Av中文字幕无码久久久妻妇 | 青青草国产精品久久| 久久w5ww成w人免费| 亚洲AV无码1区2区久久| 亚洲欧美伊人久久综合一区二区 |