接口 interface 实例

本文详细介绍了C#中接口的多种实现方式,包括属性、方法、事件等,并通过多个实例展示了如何解决同名方法和属性的冲突问题,以及如何正确地实现和调用接口。

实例一:接口可以包含-接口的属性、接口的方法、接口的自定义事件

public   interface  IBook
    {
        
string  BookName   // 接口的属性
        {
            
get ;
            
set ;
        }
        
void  InsertToDate();  // 接口的方法
         event  Modify ModifyBookName;   // 接口的自定义事件
    }

 

实例三:

interface  I_3_A
{
    
int  Count{  get set ; }
    
int  J( int  j);
}
interface  I_3_B
{
    
void  Count( int  i);
    
double  J( double  j);
}
interface  I_3_C : I_3_A, I_3_B {  object  Count(I_3_A i_3_A);}
public   class  I_3_L
{
    
public   void   Sum(I_3_C thc)
    {
        thc.Count();  
// 错误,具有二义性
        thc.Count  =   1 ;   // 错误,具有二义性
        thc.Count( 1 );   // 错误,具有二义性
        ((I_3_A)thc).Count  =   1 ;
        ((I_3_B)thc).Count(
1 );
        ((I_3_A)thc).J(
1 );
        ((I_3_B)thc).J(
1 );
        thc.J(
1.0 );
        thc.J(
1 );
    }
}

PS:  1、外部对接口的访问,如果出现同名参数或者方法,必须显示的指出他的父接口
            特别是在不清楚具体情况的前提下,最好是做的保守一点。
        2、根据参数的不同,自动选择接口里的方法

thc.J( 1.0 ); 
thc.J(
1 );

 

实例四:

interface  I_4_A
{
    
string  F( string  A);
}
interface  I_4_B : I_4_A
{
    
new   string  F( string  A);
}
interface  I_4_C : I_4_A
{
    
string  T();
}
interface  I_4_D : I_4_B, I_4_C
{ }
public   class  I_L
{
    
public   string  Test(I_4_D thc)
    {
        thc.T();
        thc.F(
" B接口的方法 " );
        ((I_4_A)thc).F(
" A接口的方法 " );
        ((I_4_C)thc).F(
" A接口的方法 " );
        ((I_4_D)thc).F(
" B接口的方法 " );
    }
}

PS: 1、thc.T();                                                D接口调用C接口的T()方法
      2、thc.F("B接口的方法");                            D接口继承自B,C接口,先查找B,C接口是否有F()方法,在B接口找到new F()方法,并调用                                                                        停止找到,不调用A接口的F()方法      
      3、((I_4_A)thc).F("A接口的方法");           
将D接口转换为A接口,调用A接口的F()方法 
      4、((I_4_C)thc).F("A接口的方法");            将D接口转换为C接口,C接口继承A接口,C无F()方法,调用A接口F()方法
      5、((I_4_D)thc).F("B接口的方法");            将D接口转换为D接口,与thc.F()一样

实例五:

ContractedBlock.gif ExpandedBlockStart.gif Code
public interface I_5_A
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
string Name getset; }
ExpandedSubBlockStart.gifContractedSubBlock.gif    
double Price getset; }
    
string X_More();
    
string More();
}

public class I_5_L:I_5_A
ExpandedBlockStart.gifContractedBlock.gif
{
    
private string name;
    
private double price;

    
public I_5_L()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        name 
= "吴有鋆";
        price 
= 66.888;
    }

ContractedSubBlock.gifExpandedSubBlockStart.gif    
I_5_A 成员#region I_5_A 成员

     
string I_5_A.Name  //显示的实现属性,显示声明没必要用修饰符
ExpandedSubBlockStart.gifContractedSubBlock.gif
    {
        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return name;
        }

        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            name 
= value;
        }

    }


    
public double Price
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return price;
        }

        
set
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            price 
= value;
        }

    }


    
string I_5_A.X_More()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
string more = name + price.ToString();
        
return more;
    }


    
public string More()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
string more = name + price.ToString();
        
return more;
    }


    
#endregion

}

PS:  1、被显示实现的接口成员,不能被从类的实例访问

       2、B类实现A接口,调用A接口的方法,去A的派生类查找最近的实现

ContractedBlock.gifExpandedBlockStart.gif Code
  I_5_L i5 = new I_5_L();
        i5.Price 
= 50.08;
        lbl_i_5_1.Text 
= i5.Price.ToString();
        lbl_i_5_2.Text 
= i5.More();

        
//要调用显示实现的接口,就必须有接口来调用
        I_5_A i5a = i5;
        i5a.Name 
= "我是接口调用的";
        lbl_i_5_3.Text 
= i5a.Name.ToString();
        lbl_i_5_4.Text 
= i5a.X_More();

 

实例六:

ContractedBlock.gif ExpandedBlockStart.gif Code
public interface I_6_A
ExpandedBlockStart.gifContractedBlock.gif
{
    
string GetUrl();
    
string GetName();
}

public class I_6_L_1:I_6_A
ExpandedBlockStart.gifContractedBlock.gif
{

ContractedSubBlock.gifExpandedSubBlockStart.gif    
I_6_A 成员#region I_6_A 成员

    
public virtual string GetUrl()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return "www.thc123.com";
    }


    
public virtual string GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return "天洪川在线培训";
    }


    
#endregion

}

//如果要显示的实现接口,就要显示的继承一次接口(虽然I_6_L_1已经对I_6_A隐式地继承)
//显示地实现接口成员是不需要修饰符的
public class I_6_L_2 : I_6_L_1, I_6_A
ExpandedBlockStart.gifContractedBlock.gif
{
ContractedSubBlock.gifExpandedSubBlockStart.gif    
I_6_A 成员#region I_6_A 成员

    
string I_6_A.GetUrl()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return "www.thcji.com";
    }


    
string I_6_A.GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return "天洪川的博客";
    }


    
#endregion

}

public class I_6_L_3 : I_6_L_1
ExpandedBlockStart.gifContractedBlock.gif
{
    
public override string GetUrl()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return "www.thc123.net";
    }

    
public override string GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return "另一个域名";
    }

}

public class I_6_L_4 : I_6_L_1
ExpandedBlockStart.gifContractedBlock.gif
{
    
public new string GetUrl()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return "没地址";
    }

    
public new string GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
return "没有名字";
    }

}

调用:

L1类实例化调用:

 I_6_L_1 i1  =   new  I_6_L_1();
        lbl_6_1.Text 
=  i1.GetUrl();
        lbl_6_2.Text 
=  i1.GetName();

lbl_6_1.Text =www.thc123.com

lbl_6_2.Text=天洪川在线培训

L2类实例化调用:

  I_6_L_2 i2  =   new  I_6_L_2();
        lbl_6_3.Text 
=  i2.GetUrl();   //显示的实现接口,等于调用接口的最近的类实现
        lbl_6_4.Text 
=  i2.GetName();

lbl_6_3.Text=www.thc123.com

lbl_6_4.Text=天洪川在线培训

L3类实例化调用:

I_6_L_3 i3  =   new  I_6_L_3();
        lbl_6_5.Text 
=  i3.GetUrl();
        lbl_6_6.Text 
=  i3.GetName();

L3类overrideL1类,所以调用L3类的方法

lbl_6_5.Text=www.thc123.net

lbl_6_6.Text=另一个域名

L4类实例化调用:

I_6_L_4 i4  =   new  I_6_L_4();
        lbl_6_7.Text 
=  i4.GetUrl();
        lbl_6_8.Text 
=  i4.GetName();

L4类new L1类,所以调用L4类的方法

lbl_6_7.Text=没地址

lbl_6_8.Text=没有名字

 

 实例九:

ContractedBlock.gif ExpandedBlockStart.gif Code
    public interface I_9_A
ExpandedBlockStart.gifContractedBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
int Age getset; }
        
string GetName();
        
string GetPwd();
    }

    
public class I_9_L_1 : I_9_A
ExpandedBlockStart.gifContractedBlock.gif    
{
        
protected int age;
        
protected string name;
        
protected string pwd;
        
public I_9_L_1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            age 
= 25;
            name 
= "吴有鋆";
            pwd 
= "www.tiger9.com";
        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
I_9_A 成员#region I_9_A 成员

        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return age;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                age 
= value;
            }

        }


        
public  string GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return name;
        }


        
public  string GetPwd()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return pwd;
        }


        
#endregion

    }

    
public class I_9_L_2:I_9_L_1
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public new string GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "New方法:" + name;
        }

        
public new string GetPwd()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "New方法:" + pwd;
        }

    }

PS:1、调用I_9_L_1类      25     吴有鋆                      www.tiger9.com

     2、调用I_9_L_2类      25     New方法:吴有鋆        New方法:www.tiger9.com

     3、调用I_9_A接口引用(映射)I_9_L_1类   25     吴有鋆                      www.tiger9.com

     4、调用I_9_A接口引用(映射)I_9_L_2类   25     吴有鋆                      http://www.tiger9.com/

修改成两个类Override方法

ContractedBlock.gif ExpandedBlockStart.gif Code
    public interface I_9_A
ExpandedBlockStart.gifContractedBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
int Age getset; }
        
string GetName();
        
string GetPwd();
    }

    
public class I_9_L_1 : I_9_A
ExpandedBlockStart.gifContractedBlock.gif    
{
        
protected int age;
        
protected string name;
        
protected string pwd;
        
public I_9_L_1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            age 
= 25;
            name 
= "吴有鋆";
            pwd 
= "www.tiger9.com";
        }

ContractedSubBlock.gifExpandedSubBlockStart.gif        
I_9_A 成员#region I_9_A 成员

        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return age;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                age 
= value;
            }

        }


        
public virtual string GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return name;
        }


        
public virtual string GetPwd()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return pwd;
        }


        
#endregion

    }

    
public class I_9_L_2:I_9_L_1
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public override string GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "New方法:" + name;
        }

        
public override string GetPwd()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "New方法:" + pwd;
        }

    }

    
public class I_9_L_3 : I_9_L_2
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public override string GetName()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "I_9_L_3 New方法" + name;
        }

        
public override string GetPwd()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return "I_9_L_3 New方法" + pwd;
        }

    }

PS:    1、调用I_9_A接口引用(映射)I_9_L_1类   25     吴有鋆                               http://www.tiger9.com/

         2、调用I_9_A接口引用(映射)I_9_L_2类   25      New方法:吴有鋆                New方法:www.tiger9.com

         3、调用I_9_A接口引用(映射)I_9_L_3类   25     I_9_L_3 New方法吴有鋆       I_9_L_3 New方法www.tiger9.com

转载于:https://www.cnblogs.com/net123/archive/2008/10/22/1316368.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值