C# 类的继承和Interface接口

对象类是对一种事物的抽象,对象是类的实例。构成类的方法和变量成为类的成员。类的定义是以关键字 class 开始,后跟类的名称。类的主体,包含在一对花括号内。类的默认访问标识符是 internal,成员的默认访问标识符是 private

C# 类的静态成员

我们可以使用 static 关键字把类成员定义为静态的。当我们声明一个类成员为静态时,意味着无论有多少个类的对象被创建,只会有一个该静态成员的副本。

关键字 static 意味着类中只有一个该成员的实例。静态变量用于定义常量,因为它们的值可以通过直接调用类而不需要创建类的实例来获取。静态变量可在成员函数或类的定义外部进行初始化。你也可以在类的定义内部初始化静态变量。

下面的实例演示了静态变量的用法:

[csharp]  view plain  copy
  1. using System;  
  2. namespace StaticVarApplication  
  3. {  
  4.     class StaticVar  
  5.     {  
  6.        public static int num;  
  7.         public void count()  
  8.         {  
  9.             num++;  
  10.         }  
  11.         public int getNum()  
  12.         {  
  13.             return num;  
  14.         }  
  15.     }  
  16.     class StaticTester  
  17.     {  
  18.         static void Main(string[] args)  
  19.         {  
  20.             StaticVar s1 = new StaticVar();  
  21.             StaticVar s2 = new StaticVar();  
  22.             s1.count();  
  23.             s1.count();  
  24.             s1.count();  
  25.             s2.count();  
  26.             s2.count();  
  27.             s2.count();           
  28.             Console.WriteLine("s1 的变量 num: {0}", s1.getNum());  
  29.             Console.WriteLine("s2 的变量 num: {0}", s2.getNum());  
  30.             Console.ReadKey();  
  31.         }  
  32.     }  
  33. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. s1 的变量 num: 6  
  2. s2 的变量 num: 6  

你也可以把一个成员函数声明为 static这样的函数只能访问静态变量。静态函数在对象被创建之前就已经存在。下面的实例演示了静态函数的用法:

[csharp]  view plain  copy
  1. using System;  
  2. namespace StaticVarApplication  
  3. {  
  4.     class StaticVar  
  5.     {  
  6.        public static int num;  
  7.         public void count()  
  8.         {  
  9.             num++;  
  10.         }  
  11.         public static int getNum()  
  12.         {  
  13.             return num;  
  14.         }  
  15.     }  
  16.     class StaticTester  
  17.     {  
  18.         static void Main(string[] args)  
  19.         {  
  20.             StaticVar s = new StaticVar();  
  21.             s.count();  
  22.             s.count();  
  23.             s.count();                     
  24.             Console.WriteLine("变量 num: {0}", StaticVar.getNum());  
  25.             Console.ReadKey();  
  26.         }  
  27.     }  
  28. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. 变量 num: 3  

基类和派生类

当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类,这个新的类被称为派生类

C# 中创建派生类的语法如下:

[csharp]  view plain  copy
  1. <acess-specifier> class <base_class>  
  2. {  
  3.  ...  
  4. }  
  5. class <derived_class> : <base_class>  
  6. {  
  7.  ...  
  8. }  

例如:

[csharp]  view plain  copy
  1. class Shape   
  2.  {  
  3.     public void setWidth(int w)  
  4.     {  
  5.        width = w;  
  6.     }  
  7.     public void setHeight(int h)  
  8.     {  
  9.        height = h;  
  10.     }  
  11.     protected int width;  
  12.     protected int height;  
  13.  }  
  14.   
  15.  // 派生类  
  16.  class Rectangle: Shape  
  17.  {  
  18.     public int getArea()  
  19.     {   
  20.        return (width * height);   
  21.     }  
  22.  }  
  23.    

基类初始化

派生类继承了基类的成员变量和成员方法。因此父类对象应在子类对象创建之前被创建。您可以在成员初始化列表中进行父类的初始化。

下面的程序演示了这点:

[csharp]  view plain  copy
  1. using System;  
  2. namespace RectangleApplication  
  3. {  
  4.    class Rectangle  
  5.    {  
  6.       // 成员变量  
  7.       protected double length;  
  8.       protected double width;  
  9.       public Rectangle(double l, double w)  
  10.       {  
  11.          length = l;  
  12.          width = w;  
  13.       }  
  14.       public double GetArea()  
  15.       {  
  16.          return length * width;  
  17.       }  
  18.       public void Display()  
  19.       {  
  20.          Console.WriteLine("长度: {0}", length);  
  21.          Console.WriteLine("宽度: {0}", width);  
  22.          Console.WriteLine("面积: {0}", GetArea());  
  23.       }  
  24.    }//end class Rectangle    
  25.    class Tabletop : Rectangle  
  26.    {  
  27.       private double cost;  
  28.       public Tabletop(double l, double w) : base(l, w)  
  29.       { }  
  30.       public double GetCost()  
  31.       {  
  32.          double cost;  
  33.          cost = GetArea() * 70;  
  34.          return cost;  
  35.       }  
  36.       public void Display()  
  37.       {  
  38.          base.Display();  
  39.          Console.WriteLine("成本: {0}", GetCost());  
  40.       }  
  41.    }  
  42.    class ExecuteRectangle  
  43.    {  
  44.       static void Main(string[] args)  
  45.       {  
  46.          Tabletop t = new Tabletop(4.5, 7.5);  
  47.          t.Display();  
  48.          Console.ReadLine();  
  49.       }  
  50.    }  
  51. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. 长度: 4.5  
  2. 宽度: 7.5  
  3. 面积: 33.75  
  4. 成本: 2362.5  

C# 多重继承

多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。

C# 不支持多重继承。但是,您可以使用接口来实现多重继承。下面的程序演示了这点:

[csharp]  view plain  copy
  1. using System;  
  2. namespace InheritanceApplication  
  3. {  
  4.    class Shape   
  5.    {  
  6.       public void setWidth(int w)  
  7.       {  
  8.          width = w;  
  9.       }  
  10.       public void setHeight(int h)  
  11.       {  
  12.          height = h;  
  13.       }  
  14.       protected int width;  
  15.       protected int height;  
  16.    }  
  17.   
  18.    // 基类 PaintCost  
  19.    public interface PaintCost   
  20.    {  
  21.       int getCost(int area);  
  22.   
  23.    }  
  24.    // 派生类  
  25.    class Rectangle : Shape, PaintCost  
  26.    {  
  27.       public int getArea()  
  28.       {  
  29.          return (width * height);  
  30.       }  
  31.       public int getCost(int area)  
  32.       {  
  33.          return area * 70;  
  34.       }  
  35.    }  
  36.    class RectangleTester  
  37.    {  
  38.       static void Main(string[] args)  
  39.       {  
  40.          Rectangle Rect = new Rectangle();  
  41.          int area;  
  42.          Rect.setWidth(5);  
  43.          Rect.setHeight(7);  
  44.          area = Rect.getArea();  
  45.          // 打印对象的面积  
  46.          Console.WriteLine("总面积: {0}",  Rect.getArea());  
  47.          Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));  
  48.          Console.ReadKey();  
  49.       }  
  50.    }  
  51. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. 总面积: 35  
  2. 油漆总成本: $2450  

C#接口

接口定义了所有类继承接口时应遵循的语法合同。接口定义了语法合同 "是什么" 部分,派生类定义了语法合同 "怎么做" 部分。

接口着重与实现什么功能,而继承着重于是否在逻辑上keyi

接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。

接口使得实现接口的类或结构在形式上保持一致。

抽象类在某种程度上与接口类似,但是,它们大多只是用在当只有少数方法由基类声明由派生类实现时。


接口的定义

接口使用 interface 关键字声明,它与类的声明类似。接口声明默认是 public 的。下面是一个接口声明的实例:

interface IMyInterface
{
    void MethodToImplement();
}

以上代码定义了接口 IMyInterface。通常接口命令以 I 字母开头,这个接口只有一个方法 MethodToImplement(),没有参数和返回值,当然我们可以安装需求设置参数和返回值。

值得注意的是,该方法并没有具体的实现。


接口的实现

接口使用 interface 关键字声明,它与类的声明类似。接口声明默认是 public 的。下面是一个接口声明的实例:

[csharp]  view plain  copy
  1. using System;  
  2.   
  3. interface IMyInterface  
  4. {  
  5.     // 接口成员  
  6.     void MethodToImplement();  
  7. }  
  8.   
  9. class InterfaceImplementer : IMyInterface  
  10. {  
  11.     static void Main()  
  12.     {  
  13.         InterfaceImplementer iImp = new InterfaceImplementer();  
  14.         iImp.MethodToImplement();  
  15.     }  
  16.   
  17.     public void MethodToImplement()  
  18.     {  
  19.         Console.WriteLine("MethodToImplement() called.");  
  20.     }  
  21. }  

InterfaceImplementer 类实现了 IMyInterface 接口,接口的实现与类的继承语法格式类似:

[csharp]  view plain  copy
  1. class InterfaceImplementer : IMyInterface  

继承接口后,我们需要实现接口的方法 MethodToImplement() , 方法名必须与接口定义的方法名一致。


接口的继承

以下实例定义了两个接口 IMyInterface 和 IParentInterface。

如果一个接口继承其他接口,那么实现类或结构就需要实现所有接口的成员。

以下实例 IMyInterface 继承了 IParentInterface 接口,因此接口实现类必须实现 MethodToImplement() 和 ParentInterfaceMethod() 方法:

[csharp]  view plain  copy
  1. using System;  
  2.   
  3. interface IParentInterface  
  4. {  
  5.     void ParentInterfaceMethod();  
  6. }  
  7.   
  8. interface IMyInterface : IParentInterface  
  9. {  
  10.     void MethodToImplement();  
  11. }  
  12.   
  13. class InterfaceImplementer : IMyInterface  
  14. {  
  15.     static void Main()  
  16.     {  
  17.         InterfaceImplementer iImp = new InterfaceImplementer();  
  18.         iImp.MethodToImplement();  
  19.         iImp.ParentInterfaceMethod();  
  20.     }  
  21.   
  22.     public void MethodToImplement()  
  23.     {  
  24.         Console.WriteLine("MethodToImplement() called.");  
  25.     }  
  26.   
  27.     public void ParentInterfaceMethod()  
  28.     {  
  29.         Console.WriteLine("ParentInterfaceMethod() called.");  
  30.     }  
  31. }  

实例输出结果为:

[csharp]  view plain  copy
  1. MethodToImplement() called.  
  2. ParentInterfaceMethod() called.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值