C# 教程
- 第1节:C# 简介
- 第2节:C# 环境
- 第3节:C# 程序结构
- 第4节:C# 基本语法
- 第5节:C# 数据类型
- 第6节:C# 类型转换
- 第7节:C# 变量
- 第8节:C# 常量
- 第9节:C# 运算符
- 第10节:C# 判断
- 第11节:C# 循环
- 第12节:C# 封装
- 第13节:C# 方法
- 第14节:C# 可空类型(Nullable)
- 第15节:C# 数组(Array)
- 第16节:C# 字符串(String)
- 第17节:C# 结构(Struct)
- 第18节:C# 枚举(Enum)
- 第19节:C# 类(Class)
- 第20节:C# 继承
- 第21节:C# 多态性
- 第22节:C# 运算符重载
- 第23节:C# 接口(Interface)
- 第24节:C# 命名空间(Namespace)
- 第25节:C# 预处理器指令
- 第26节:C# 正则表达式
- 第27节:C# 异常处理
- 第28节:C# 文件的输入与输出
- 第29节:C# 特性(Attribute)
- 第30节:C# 反射(Reflection)
- 第31节:C# 属性(Property)
- 第32节:C# 索引器(Indexer)
- 第33节:C# 委托(Delegate)
- 第34节:C# 事件(Event)
- 第35节:C# 集合(Collection)
- 第36节:C# 泛型(Generic)
- 第37节:C# 匿名方法
- 第38节:C# 不安全代码
- 第39节:C# 多线程
C# 继承
继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类,这使得创建和维护应用程序变得更容易。同时也有利于重用代码和节省开发时间。
当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类,这个新的类被称为派生类。
继承的思想实现了 属于(IS-A) 关系。例如,哺乳动物 属于(IS-A) 动物,狗 属于(IS-A) 哺乳动物,因此狗 属于(IS-A) 动物。
基类和派生类
派生类只能有一个直接基类,但一个基类可以有多个直接派生类。继承是可以传递的。定义要从其他类派生的类时,派生类会隐式获得基类的所有成员(除了其构造函数和终结器)。派生类因而可以重用基类中的代码,而无需重新实现。在派生类中,可以添加更多成员。通过这种方法,派生类可扩展基类的功能。
C# 中创建派生类的语法如下:
<acess-specifier> class <base_class>{
...
}
class <derived_class> : <base_class>{
...
}
假设,有一个基类 Shape,它的派生类是 Rectangle:
using System;
namespace InheritanceApplication{
class Shape {
public void setWidth(int w){
width = w;
}
public void setHeight(int h){
height = h;
}
protected int width;
protected int height;
}
// 派生类
class Rectangle: Shape{
public int getArea(){
return (width * height);
}
}
class RectangleTester{
static void Main(string[] args){
Rectangle Rect = new Rectangle();
Rect.setWidth(5);
Rect.setHeight(7);
// 打印对象的面积
Console.WriteLine("总面积: {0}", Rect.getArea());
Console.ReadKey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
总面积: 35
基类的初始化
派生类继承了基类的成员变量和成员方法。因此父类对象应在子类对象创建之前被创建。您可以在成员初始化列表中进行父类的初始化。
下面的程序演示了这点:
using System;
namespace RectangleApplication{
class Rectangle{
// 成员变量
protected double length;
protected double width;
public Rectangle(double l, double w){
length = l;
width = w;
}
public double GetArea(){
return length * width;
}
public void Display(){
Console.WriteLine("长度: {0}", length);
Console.WriteLine("宽度: {0}", width);
Console.WriteLine("面积: {0}", GetArea());
}
}//end class Rectangle
class Tabletop : Rectangle{
private double cost;
public Tabletop(double l, double w) : base(l, w){ }
public double GetCost(){
double cost;
cost = GetArea() * 70;
return cost;
}
public void Display(){
base.Display();
Console.WriteLine("成本: {0}", GetCost());
}
}
class ExecuteRectangle{
static void Main(string[] args){
Tabletop t = new Tabletop(4.5, 7.5);
t.Display();
Console.ReadLine();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
长度: 4.5
宽度: 7.5
面积: 33.75
成本: 2362.5
C# 多重继承
C# 不支持多重继承。但是,您可以使用接口来实现多重继承。下面的程序演示了这点:
using System;
namespace InheritanceApplication{
class Shape {
public void setWidth(int w){
width = w;
}
public void setHeight(int h){
height = h;
}
protected int width;
protected int height;
}
// 基类 PaintCost
public interface PaintCost {
int getCost(int area);
}
// 派生类
class Rectangle : Shape, PaintCost{
public int getArea(){
return (width * height);
}
public int getCost(int area){
return area * 70;
}
}
class RectangleTester{
static void Main(string[] args){
Rectangle Rect = new Rectangle();
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// 打印对象的面积
Console.WriteLine("总面积: {0}", Rect.getArea());
Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}
当上面的代码被编译和执行时,它会产生下列结果:
总面积: 35
油漆总成本: $2450