• <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
            數據加載中……

            C#中的繼承與覆蓋

            C#中繼承與函數覆蓋,有時比較復雜,不小心很容易弄錯。所以有必要總結點規律出來。
            (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";
                  
             
                    
            //當子類含有和父類相同的函數簽名(可見性public或者protected、函數名和參數都相同)時,不管帶new與否,都將覆蓋父類中相同的函數

                    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小時,只會對作用范圍內的部分產生覆蓋影響,對作用范圍外無影響。
            如下例:
            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內部的函數有影響,出了Chicken類,該函數不具有作用。
                    private new string type = "Chicken";

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

                    
            //這里的ShowType()函數只會對Chicken內部的函數有影響,出了Chicken類,該函數不具有作用。
                    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()");
                        
            //這里會調用Chicken的type成員
                        Console.WriteLine(type);
                        
            //這里將調用Chicken的ShowType()函數
                        ShowType();
                    }

                }


                
            public class TestInheritance
                
            {     

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

                        
            //這里會調用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名字相同但參數不同時,不會產生覆蓋。如:
            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內部的函數有影響,出了Chicken類,該函數不具有作用。
                    protected new string type = "Chicken";

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

                 
                    
            public void ShowInfo()
                    
            {
                        
            //會調用Bird的ShowType函數
                        Console.WriteLine();
                        Console.WriteLine(
            "Chicken::ShowInfo()");
                        ShowType();           
                    }

                }


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

                }


                
            public class TestInheritance
                
            {     

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

                        
            //這里會調用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。

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

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

            性色欲网站人妻丰满中文久久不卡| 亚洲精品国产自在久久| 久久久久久久久久免免费精品| 久久久久亚洲AV无码专区桃色| 伊人久久大香线蕉av不变影院| 精品久久久久久国产| 亚洲国产成人精品女人久久久| 久久超乳爆乳中文字幕| 欧美激情精品久久久久久久九九九| 久久久久久久91精品免费观看| 精品精品国产自在久久高清| 久久综合亚洲色HEZYO国产| 无遮挡粉嫩小泬久久久久久久| 美女写真久久影院| 色婷婷久久综合中文久久蜜桃av| 亚洲国产成人精品女人久久久| 无码国内精品久久人妻蜜桃 | 久久99国产精品久久99果冻传媒| 久久av高潮av无码av喷吹| 狠狠色丁香婷婷综合久久来| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 热综合一本伊人久久精品| 久久精品国产亚洲av麻豆色欲| 久久精品国产欧美日韩99热| 91精品国产91久久久久久| 国产精品亚洲美女久久久| 国产女人aaa级久久久级| A狠狠久久蜜臀婷色中文网| 少妇人妻88久久中文字幕| 亚洲精品久久久www| 久久久国产精品| 久久AⅤ人妻少妇嫩草影院| 日本精品久久久中文字幕| 狠狠久久亚洲欧美专区| 久久综合综合久久97色| 国产精品99久久久久久人| 国产巨作麻豆欧美亚洲综合久久| 欧美精品一本久久男人的天堂| 国产成人精品免费久久久久| 久久久久亚洲精品无码蜜桃 | 国产精品久久毛片完整版|