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

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

 

实例三:

interface I_3_A
{
    
int Count{ getset; }
    
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()一样

实例五:

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

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

 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();

 

实例六:

}

调用:

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=没有名字

 

 实例九:

    }

PS:1、调用I_9_L_1类      www.tiger9.com

     2、调用I_9_L_2类      New方法:www.tiger9.com

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

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

修改成两个类Override方法

    }

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

         2、调用I_9_A接口引用(映射)I_9_L_2类   25      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

相关文章:

  • 2022-03-02
  • 2021-09-06
  • 2022-02-18
  • 2021-08-06
  • 2021-08-02
  • 2021-06-11
猜你喜欢
  • 2022-12-23
  • 2021-12-24
  • 2021-12-31
  • 2021-08-14
  • 2021-12-09
  • 2021-09-06
相关资源
相似解决方案