第十七讲:继承

来源:互联网 发布:dnf属性强化数据 编辑:程序博客网 时间:2024/04/19 13:00
2. 继承性一个类可以有能力直接从另一个类获得代码或数据派生类从基类那里获得其所有成员能够从父类中获得方法域属性事件索引指示器不能获得构造函数和析构函数C#只允许单继承3. 实现继承例:public class soldgoods:goodsC#中只支持单继承防止继承public sealed class xxx注意:关键字sealed4. 继承透视图5. 访问基类成员(即子类访问父类)base关键字用于访问父类的成员this关键字用于访问本类的成员派生类可以调用基类的方法通过使用base关键字派生类在访问基类的时候有一定的限制,不能访问private的成员,internal的基类成员只能                被同一个程序集中的派生类访问6. 如何实现多继承迂回方式,B:A C:B 那么C就可以使用A和B中的属性方法了7. 对象引用与类型转换定义车类卡车和客车继承于车类桥车继承于客车类实例时:车类 i = new 卡车类 //卡车是车但是:卡车 i2 = new 车类 //车是卡车,是错误必须:卡车 i2 = (卡车类)i8. 密封类在类申明关键字前面使用sealed关键字即可重写基类中的虚方法和虚属性,即方法或属性上使用sealed修饰符。这将使您能够允许类从您的类继承,        并防止它们重写特定的虚方法或虚属性。如下图:

原代码:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Inherization{    /// <summary>    /// 动物类    /// </summary>    public class Animal    {        /// <summary>        /// 属性:性别        /// </summary>        public int sex        {            get;            set;        }        /// <summary>        /// 属性:皮肤颜色        /// </summary>        public int color        {            get;            set;        }        /// <summary>        /// 方法:吃        /// </summary>        public void Chi()        {                    }        /// <summary>        /// 方法:喝        /// </summary>        public void He()        {            throw new System.NotImplementedException();        }        /// <summary>        /// 方法:玩        /// </summary>        public void Wan()        {            throw new System.NotImplementedException();        }    }    /// <summary>    /// 人类 继承自动物类    /// </summary>    public class Ren : Animal    {        /// <summary>        /// 衣服品牌        /// </summary>        public int YiFu        {            get;            set;        }        /// <summary>        /// 鞋子品牌        /// </summary>        public int Xiezi        {            get;            set;        }            /// <summary>        /// 方法:打        /// </summary>        public void Da()        {            throw new System.NotImplementedException();        }        /// <summary>        /// 方法:骂        /// </summary>        public void Ma()        {            throw new System.NotImplementedException();        }        /// <summary>        /// 方法:说话        /// </summary>        public void Stalk()        {            throw new System.NotImplementedException();        }    }    /// <summary>    /// 家禽 继承自动物类    /// </summary>    public class JiaQin : Animal    {        /// <summary>        /// 属性:睡        /// </summary>        public int shui        {            get;            set;        }            /// <summary>        /// 方法:叫喊        /// </summary>        public void Jiao()        {            throw new System.NotImplementedException();        }    }    /// <summary>    /// 野兽 继承自动物类    /// </summary>    public class YeShou : Animal    {        public int MaoPi        {            get;            set;        }            /// <summary>        /// 方法:捕食        /// </summary>        public void BuShi()        {            throw new System.NotImplementedException();        }    }    /// <summary>    /// 好人的类 继承自人类    /// </summary>    public class GoodMen : Ren    {        /// <summary>        /// 方法:救人        /// </summary>        public void JiuRen()        {            throw new System.NotImplementedException();        }    }    /// <summary>    /// 坏人的类 继承自人类    /// </summary>    public class BadMen : Ren    {        /// <summary>        /// 方法:杀人        /// </summary>        public void ShaRen()        {            throw new System.NotImplementedException();        }    }}


0 0
原创粉丝点击