先介绍一个包含继承所有要素的例子:
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
}
}
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
}
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