程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#基礎知識整理:基礎知識(4)繼承

C#基礎知識整理:基礎知識(4)繼承

編輯:C#入門知識

繼承實際是一個類對另一個類的擴展,後者稱之為基類,前者稱之為子類。繼承就是子類擁有基類的一切屬性和方法,子類還可以增加屬性和方法。但是子類不能去掉父類的屬性和方法。
    當然這裡還要提到修飾符的問題,子類擁有基類的所有屬性和方法,不意味著子類可以任意訪問繼承的這些屬性和方法。子類只能訪問到public和protected修飾的屬性和方法,其余無法直接訪問。還有一個就是static的屬性和方法是不能被繼承下來的,因為靜態類型之和類有關系與對象無關。
看代碼:

[csharp]
using System; 
 
namespace YYS.CSharpStudy.MainConsole 

    public class YSchool 
    { 
        private int id = 0; 
 
        private string name = string.Empty; 
 
        public int ID 
        { 
            get 
            { 
                return this.id; 
            } 
        } 
 
        public string Name 
        { 
            get 
            { 
                return name; 
            } 
        } 
       /// <summary>  
       /// 構造器  
       /// </summary>  
        public YSchool() 
        { 
            this.id = 0; 
 
            this.name = @"清華大學附中"; 
        } 
        /// <summary>  
        /// 構造器  
        /// </summary>  
        public  YSchool(int id, string name) 
        { 
            this.id = id; 
 
            this.name = name; 
        } 
        /// <summary>  
        /// 構造器  
        /// </summary>  
        public  YSchool(int id) 
        { 
            this.id = id; 
 
            this.name = @"陝師大附中"; 
        } 
    } 
 
    public class YTeacher 
    { 
        private int id = 0; 
 
        private string name = string.Empty; 
 
        private YSchool school = null; 
 
        private string introDuction = string.Empty; 
 
        private string imagePath = string.Empty; 
 
        /// <summary>  
        /// 使用只讀屬性,因為是固有屬性。  
        /// </summary>  
        public int ID 
        { 
            get 
            { 
                return id; 
            } 
        } 
 
        public string Name 
        { 
            get 
            { 
                return name; 
            } 
        } 
 
        /// <summary>  
        /// 這幾個使用get/set屬性,因為這幾個屬性不是固有的,可以隨著某些條件改變的。  
        /// </summary>  
        public YSchool School 
        { 
            get 
            { 
                if (school == null) 
                { 
                    school = new YSchool(); 
                } 
                return school; 
            } 
 
            set 
            { 
                school = value; 
            } 
        } 
 
        public string IntroDuction 
        { 
            get 
            { 
                return introDuction; 
            } 
 
            set 
            { 
                introDuction = value; 
            } 
        } 
 
        public string ImagePath 
        { 
            get 
            { 
                return imagePath; 
            } 
 
            set 
            { 
                imagePath = value; 
            } 
        } 
 
        public YTeacher(int id, string name) 
        { 
            this.id = id; 
 
            this.name = name; 
        } 
 
        /// <summary>  
        /// 給學生講課的方法  
        /// </summary>  
        public void ToTeachStudents() 
        { 
            Console.WriteLine(string.Format(@"{0} 老師教育同學們: Good Good Study,Day Day Up!", this.name)); 
        } 
        /// <summary>  
        /// 懲罰犯錯誤學生的方法  
        /// </summary>  
        /// <param name="punishmentContent"></param>  
        public void PunishmentStudents(string punishmentContent) 
        { 
            Console.WriteLine(string.Format(@"{0} 的{1} 老師讓犯錯誤的學生 {2}。", this.School.Name, this.name, punishmentContent)); 
        } 
    } 
    /// <summary>  
    /// 男性老師,繼承自YTeacher  
    /// </summary>  
    public class MrTeacher : YTeacher 
    { 
        /// <summary>  
        /// 構造器,這裡要注意,YTeacher是沒有提供默認構造器的,  
        /// 所以子類必須要有構造器,並且和基類參數列表一致。  
        /// </summary>  
        /// <param name="id"></param>  
        /// <param name="name"></param>  
        public MrTeacher(int id, string name) 
 
            : base(id, name) 
        { 
 
        } 
        /// <summary>  
        /// 擴展的方法,刮胡子方法。  
        /// </summary>  
        public void Shave() 
        { 
            Console.WriteLine(string.Format(@"{0} 老師用飛科剃須刀刮胡子。",this.Name)); 
        } 
    } 
    /// <summary>  
    /// 女性老師,繼承自YTeacher  
    /// </summary>  
    public class MisTeacher : YTeacher 
    { 
        /// <summary>  
        /// 構造函數  
        /// </summary>  
        /// <param name="id"></param>  
        /// <param name="name"></param>  
        public MisTeacher(int id, string name) 
 
            : base(id, name) 
        { 
 
        } 
        /// <summary>  
        /// 擴展方法,護膚的方法  
        /// </summary>  
        public void SkinCare() 
        { 
            Console.WriteLine(string.Format(@"{0} 老師用香奈兒護膚霜護膚。",this.Name)); 
        } 
    } 

using System;

namespace YYS.CSharpStudy.MainConsole
{
    public class YSchool
    {
        private int id = 0;

        private string name = string.Empty;

        public int ID
        {
            get
            {
                return this.id;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }
       /// <summary>
       /// 構造器
       /// </summary>
        public YSchool()
        {
            this.id = 0;

            this.name = @"清華大學附中";
        }
        /// <summary>
        /// 構造器
        /// </summary>
        public  YSchool(int id, string name)
        {
            this.id = id;

            this.name = name;
        }
        /// <summary>
        /// 構造器
        /// </summary>
        public  YSchool(int id)
        {
            this.id = id;

            this.name = @"陝師大附中";
        }
    }

    public class YTeacher
    {
        private int id = 0;

        private string name = string.Empty;

        private YSchool school = null;

        private string introDuction = string.Empty;

        private string imagePath = string.Empty;

        /// <summary>
        /// 使用只讀屬性,因為是固有屬性。
        /// </summary>
        public int ID
        {
            get
            {
                return id;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        /// <summary>
        /// 這幾個使用get/set屬性,因為這幾個屬性不是固有的,可以隨著某些條件改變的。
        /// </summary>
        public YSchool School
        {
            get
            {
                if (school == null)
                {
                    school = new YSchool();
                }
                return school;
            }

            set
            {
                school = value;
            }
        }

        public string IntroDuction
        {
            get
            {
                return introDuction;
            }

            set
            {
                introDuction = value;
            }
        }

        public string ImagePath
        {
            get
            {
                return imagePath;
            }

            set
            {
                imagePath = value;
            }
        }

        public YTeacher(int id, string name)
        {
            this.id = id;

            this.name = name;
        }

        /// <summary>
        /// 給學生講課的方法
        /// </summary>
        public void ToTeachStudents()
        {
            Console.WriteLine(string.Format(@"{0} 老師教育同學們: Good Good Study,Day Day Up!", this.name));
        }
        /// <summary>
        /// 懲罰犯錯誤學生的方法
        /// </summary>
        /// <param name="punishmentContent"></param>
        public void PunishmentStudents(string punishmentContent)
        {
            Console.WriteLine(string.Format(@"{0} 的{1} 老師讓犯錯誤的學生 {2}。", this.School.Name, this.name, punishmentContent));
        }
    }
    /// <summary>
    /// 男性老師,繼承自YTeacher
    /// </summary>
    public class MrTeacher : YTeacher
    {
        /// <summary>
        /// 構造器,這裡要注意,YTeacher是沒有提供默認構造器的,
        /// 所以子類必須要有構造器,並且和基類參數列表一致。
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public MrTeacher(int id, string name)

            : base(id, name)
        {

        }
        /// <summary>
        /// 擴展的方法,刮胡子方法。
        /// </summary>
        public void Shave()
        {
            Console.WriteLine(string.Format(@"{0} 老師用飛科剃須刀刮胡子。",this.Name));
        }
    }
    /// <summary>
    /// 女性老師,繼承自YTeacher
    /// </summary>
    public class MisTeacher : YTeacher
    {
        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public MisTeacher(int id, string name)

            : base(id, name)
        {

        }
        /// <summary>
        /// 擴展方法,護膚的方法
        /// </summary>
        public void SkinCare()
        {
            Console.WriteLine(string.Format(@"{0} 老師用香奈兒護膚霜護膚。",this.Name));
        }
    }
}[csharp] 
using System; 
 
namespace YYS.CSharpStudy.MainConsole 

     
    class Program 
    { 
        static void Main(string[] args) 
        { 
            MrTeacher mrTeacher = new MrTeacher(1, @"牛轟轟"); 
 
            mrTeacher.ToTeachStudents(); 
 
            mrTeacher.PunishmentStudents(@"背唐詩"); 
 
            mrTeacher.Shave();   www.2cto.com
 
            MisTeacher misTeacher = new MisTeacher(2, @"郝漂靓"); 
 
            misTeacher.ToTeachStudents(); 
 
            misTeacher.PunishmentStudents(@"默寫紅樓夢"); 
 
            misTeacher.SkinCare(); 
 
            Console.ReadKey(); 
        } 
    } 
using System;

namespace YYS.CSharpStudy.MainConsole
{
   
    class Program
    {
        static void Main(string[] args)
        {
            MrTeacher mrTeacher = new MrTeacher(1, @"牛轟轟");

            mrTeacher.ToTeachStudents();

            mrTeacher.PunishmentStudents(@"背唐詩");

            mrTeacher.Shave();

            MisTeacher misTeacher = new MisTeacher(2, @"郝漂靓");

            misTeacher.ToTeachStudents();

            misTeacher.PunishmentStudents(@"默寫紅樓夢");

            misTeacher.SkinCare();

            Console.ReadKey();
        }
    }
結果:
 

繼承是面向對象的特性,它的好處是:
第一、做項目設計類時,繼承可以讓我們省略量代碼;
第二、符合面向對象中類的組織形式,在C#、java等高級面向對象語言中,都有一個object類,這是所有類的祖宗類,其他類都有他派生而來。
繼承需要注意一點,就是子類的構造器問題。程序運行過程是子類首先調用父類的構造器。如果子類沒有寫構造器,那麼就會調用父類的默認構造器。如果父類沒有默認的構造器,即父類寫了帶參數的構造器,那麼子類也就調用帶參數的那個構造器,不過需要指明是調用的哪個帶參數的構造器(見代碼中base)。

 作者:yysyangyangyangshan

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved