[导入]继承示例

 先介绍一个包含继承所有要素的例子:

InheritExample
using ...System;

namespace Donis.CSharpBook
...{
    public class Starter
    ...{
        Main()#region Main()
        public static void Main()
        ...{

            XParent parent = new XParent();
            parent.MethodA();
            XChild child = new XChild();
            child.MethodA();
            child.MethodB();
            child.FieldA = 10;
            Console.WriteLine(child.FieldA);
            Console.ReadKey();
        }

        #endregion


        XParent#region XParent
        public class XParent
        ...{
            public void MethodA()          //一个方法。
            ...{
                Console.WriteLine("XParent.MethodA called from ...{0}.",
                    this.GetType().ToString());
            }


            private int propFieldA;          //一个字段。

            public int FieldA                   //一个属性
            ...{
                get
                ...{
                    return propFieldA;
                }

                set
                ...{
                    propFieldA = value;
                }

            }


        }

        #endregion


        XChild : XParnt#region XChild : XParnt
        public class XChild : XParent
        ...{
            public int MethodB()         //添加了一个方法。
            ...{
                Console.WriteLine("XChild.MethodB called from ...{0}.",
                    this.GetType().ToString());
                return fieldb;
            }


            private int fieldb = 5;            //添加了一个字段。
        }

        #endregion

    }

}

这个例子输出的结果是:

第一行是   parent.MethodA(); 输出的。
第二行是 child.MethodA();输出的。说明继承了,XParent中的方法。
第三行是 child.MethodB();输出的。XChild通过向集合添加一个方法来扩展XParent。
child.FieldA = 10;继承了属性。

下面是一个完整的列表,包括System.Object类中被重写的方法:

Employee
using ...System;
using System.Collections;

//System.Collections 命名空间包含接口和类,这些接口和类定义各种对象(如列表、队列、位数组、哈希表和字典)的集合。  

namespace sliuqin.CS
...{
    public class Staerter
    ...{
        public static void Main()
        ...{
            Employee obj1 = new Employee(5678);
            Employee obj2 = new Employee(5678);
            if (obj1 == obj2)
            ...{
                Console.WriteLine("equals");
            }

            else
            ...{
                Console.WriteLine("not equals");
            }

            Console.ReadKey();
        }


    }

    Employee类#region Employee类
    class Employee
    ...{
        public Employee(int id)
        ...{
            if ((id < 1000) || (id > 9999))
            ...{
                throw new Exception("INvalid Employee ID");
            }

            propID = id;
        }

        public static bool operator ==(Employee obj1, Employee obj2)
        ...{
            return obj1.Equals(obj2);
        }

        public static bool operator !=(Employee obj1, Employee obj2)
        ...{
            return !obj1.Equals(obj2);
        }



        属性#region 属性
        First#region First
        private string propFirst;
        public string First
        ...{
            get
            ...{
                return propFirst;
            }

            set
            ...{
                propFirst = value;
            }

        }

                #endregion


        Last#region Last
        private string propLast;
        public string Last
        ...{
            get
            ...{
                return propLast;
            }

            set
            ...{
                propLast = value;
            }

        }

        #endregion


        EmplID#region EmplID
        private readonly int propID;
        public int EmplID
        ...{
            get
            ...{
                return propID;
            }

        }

        #endregion


        FullName#region FullName
        public string FullName
        ...{
            get
            ...{
                return propFirst + " " + propLast;
            }

        }

        #endregion
  
        #endregion


        重写 Equals () GetHashCode() 和 ToString()#region 重写 Equals () GetHashCode() 和 ToString()
        public override bool Equals(object obj)
        ...{
            Employee _obj = obj as Employee;
            if (obj == null)
            ...{
                return false;
            }

            return this.GetHashCode() == _obj.GetHashCode();
        }

        public override int GetHashCode()
        ...{
            return EmplID;
        }


        public override string ToString()
        ...{
            return FullName;
        }
  
        #endregion

    }
  
    #endregion

}

文章来源: http://www.link-to.cn/post/2007/10/InheritExample.aspx

转载于:https://www.cnblogs.com/sliuqin/archive/2007/10/16/933621.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值