C#-接口的应用范例

来源:互联网 发布:世界银行的数据库 编辑:程序博客网 时间:2024/06/08 05:05
C#-接口的应用范例
1:接口的定义极其应用
定义接口必须在声明的接口前使用interface关键字指定这是一个接口类型,然后在接口内声明所要的方法,例如
interface Itype{
   void add();
   void sub();
.....
}
接口可以被一个以上的类继承,但是继承接口的类必须实现接口中所定义的所有方法,既然我们知道接口需要由外部类来实现其所有的方法,那么由此可知接口里的方法就必须为public类型的。下面我们就来通过一个简单的范例来看一下接口的声明极其实现
范例一
代码如下
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {

    caculator c=new caculator();
    c.add(6,2);
    c.sub(6,2);
    c.mul(6,2);
    c.div(6,2);
   }
}
interface Itype{
     void add(int a,int b);
   void sub(int a,int b);
   void mul(int a,int b);
   void div(int a,int b);
}
class caculator :Itype{
   public void add(int a,int b){
      int r=a+b;
    Console.WriteLine("{0}+{1}={2}",a,b,r);
   }
   public void sub(int a,int b){
      int r=a-b;
    Console.WriteLine("{0}-{1}={2}",a,b,r);
   }
   public void mul(int a,int b){
      int r=a*b;
    Console.WriteLine("{0}*{1}={2}",a,b,r);
   }
   public void div(int a,int b){
    if(b==0)
    {
     Console.WriteLine("input error");
    }else{
     int r=a/b;
     Console.WriteLine("{0}/{1}={2}",a,b,r);
    }
   }

}

}
运行结果
6+2=8
6-2=4
6*2=12
6/2=3
Press any key to continue

2:as关键字的使用
我们可以利用关键字as将类的实例对象铸造成为接口类型。下面我们就来通过范例来理解以下as 的应用,
范例2:我们利用as关键字将类caculator的对象c铸造成为接口Itype类型。我们只需要把上面范例的main()方法内的代码改写成为下面的代码就行了。

   static void Main(string[] args)
   {

    caculator c=new caculator();
    Itype type=c as Itype;
    type.add(6,2);
    type.sub(6,2);
    type.mul(6,2);
    type.div(6,2);
   }

运行结果与上述范例一样

3:is关键字的应用
为了防止发生接口转型的错误,我们可以利用is关键字取得转型结果,请看下面的范例
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {

    caculator c=new caculator();
    if(c is Itype){
    Itype type=(Itype) c;
    type.add(6,2);
    type.sub(6,2);
    type.mul(6,2);
    type.div(6,2);
    }
   
   }
}
interface Itype{
     void add(int a,int b);
   void sub(int a,int b);
   void mul(int a,int b);
   void div(int a,int b);
}
class caculator :Itype{
   public void add(int a,int b){
      int r=a+b;
    Console.WriteLine("{0}+{1}={2}",a,b,r);
   }
   public void sub(int a,int b){
      int r=a-b;
    Console.WriteLine("{0}-{1}={2}",a,b,r);
   }
   public void mul(int a,int b){
      int r=a*b;
    Console.WriteLine("{0}*{1}={2}",a,b,r);
   }
   public void div(int a,int b){
    if(b==0)
    {
     Console.WriteLine("input error");
    }else{
     int r=a/b;
     Console.WriteLine("{0}/{1}={2}",a,b,r);
    }
   }

}

}
运行结果与上述范例一样

4:继承多个接口
我们知道c#和java一样不支持多重继承,不过我们可以通过接口来实现多重继承,请看下面的范例
范例
代码如下
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
          caculator c=new caculator();
    c.add(6,2);
    c.sub(6,2);
    c.mul(6,2);
    c.div(6,2);
   }
}
interface Itype1{
     void add(int a,int b);
   void sub(int a,int b);
}
interface Itype2{
     void mul(int a,int b);
   void div(int a,int b);
}
class caculator :Itype1,Itype2{
   public void add(int a,int b)
   {
    int r=a+b;
    Console.WriteLine("{0}+{1}={2}",a,b,r);
   }
   public void sub(int a,int b)
   {
    int r=a-b;
    Console.WriteLine("{0}-{1}={2}",a,b,r);
   }
   public void mul(int a,int b)
   {
    int r=a*b;
    Console.WriteLine("{0}*{1}={2}",a,b,r);
   }
   public void div(int a,int b)
   {
    if(b==0)
    {
     Console.WriteLine("input error");
    }
    else
    {
     int r=a/b;
     Console.WriteLine("{0}/{1}={2}",a,b,r);
    }
   }
}

}
运行结果
6+2=8
6-2=4
6*2=12
6/2=3
Press any key to continue

接口的扩展
实现接口的扩展和实现类的扩展的方式是一样的,在这里我们就不在详细说明了,请看下面的范例。
代码如下]
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
          caculator c=new caculator();
    c.add(6,2);
    c.sub(6,2);
    c.mul(6,2);
    c.div(6,2);
   }
}
interface Itype1{
     void add(int a,int b);
   void sub(int a,int b);
}
interface Itype2:Itype1{
     void mul(int a,int b);
   void div(int a,int b);
}
class caculator :Itype2{
   public void add(int a,int b)
   {
    int r=a+b;
    Console.WriteLine("{0}+{1}={2}",a,b,r);
   }
   public void sub(int a,int b)
   {
    int r=a-b;
    Console.WriteLine("{0}-{1}={2}",a,b,r);
   }
   public void mul(int a,int b)
   {
    int r=a*b;
    Console.WriteLine("{0}*{1}={2}",a,b,r);
   }
   public void div(int a,int b)
   {
    if(b==0)
    {
     Console.WriteLine("input error");
    }
    else
    {
     int r=a/b;
     Console.WriteLine("{0}/{1}={2}",a,b,r);
    }
   }
}

}
运行结果
6+2=8
6-2=4
6*2=12
6/2=3
Press any key to continue

避免方法的存取冲突
我们知道一个类允许同时继承多个接口,这样就造成了一个问题,如果两个接口中的方法名称相同是很有可能的问题,那么我们如何解决这样中问题那,?为了解决这个问题我们必须强制接口实现,也就是明确指定方法接口。请看下面的范例
代码如下
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
          caculator c=new caculator();
    c.add(6,2);
    c.sub(6,2);

            Itype2 type= c as Itype2;
    type.add(2.2,2.2);
    type.sub(2.2,2.2);

   }
}
interface Itype1{
      void add(int a,int b);
   void sub(int a,int b);
}
interface Itype2{
     void add(double a,double b);
   void sub(double a,double b);
}
class caculator :Itype1,Itype2{
   public void add(int a,int b)
   {
    int r=a+b;
    Console.WriteLine("{0}+{1}={2}",a,b,r);
   }
   public void sub(int a,int b)
   {
    int r=a-b;
    Console.WriteLine("{0}-{1}={2}",a,b,r);
   }
   void Itype2.add(double a,double b){
      double r=a+b;
    Console.WriteLine("{0}+{1}={2}",a,b,r);
   }
   void Itype2.sub(double a,double b){
      double r=a-b;
    Console.WriteLine("{0}-{1}={2}",a,b,r);
   }
  
}

}
运行结果
6+2=8
6-2=4
2.2+2.2=4.4
2.2-2.2=0
Press any key to continue


结构(struct)应用范例
结构是一种可以让你利用数值类型的方式直接存取对象,而不用需要对象的参考,这在于某些小型对象的使用上非常方便,并且可以提供比类对象更出色的习惯年能,由于结构本身是一种数值类型,因此系统不需要象使用类一样使用new运算符来产生实例对象,不用并不代表着结构不能使用new运算符产生实例对象,下面请看几个范例
范例一]
代码如下
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
            structTest st;
    st.name="Jim";
    st.number=123;
    st.age=20;
    st.sex="boy";
    st.show();
            Console.WriteLine("______________________________");
    structTest st1=new structTest("Mary",456,"girl",21);
   }
}
struct structTest{
     public string name;
     public int number;
   public string sex;
   public int age;
   public structTest(string name,int number,string sex,int age){
      this.name=name;
    this.number=number;
    this.sex=sex;
    this.age=age;
    Console.WriteLine("Name:-->"+name);
    Console.WriteLine("Number-->"+number);
    Console.WriteLine("Sex:-->"+sex);
    Console.WriteLine("Age-->"+age);
   }
   public void show(){
      Console.WriteLine("Name:-->"+name);
    Console.WriteLine("Number-->"+number);
    Console.WriteLine("Sex:-->"+sex);
    Console.WriteLine("Age-->"+age);
   }
}

}
运行结果
Name:-->Jim
Number-->123
Sex:-->boy
Age-->20
______________________________
Name:-->Mary
Number-->456
Sex:-->girl
Age-->21
Press any key to continue
2:结构的嵌套:
代码如下
using System;

namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
   /// <summary>
   /// 应用程序的主入口点。
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
           outStruct outs;
    outs.a=10;
    Console.WriteLine("Out Struct a-->"+outs.a);

    outStruct.innerStruct inner;
    inner.b=20;
            Console.WriteLine("_____________________________");
    Console.WriteLine("Inner Struct b-->"+inner.b);
   }
}
struct outStruct{
    public int a;
   public struct innerStruct{
      public int b;

   }
}

}
运行结果
Out Struct a-->10
_____________________________
Inner Struct b-->20
Press any key to continue

原创粉丝点击