• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
我的学习笔记
   首页       联系   管理    订阅  订阅

接口 interface 实例

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

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()一样

实例五:

Code
public interface I_5_A
{
    
string Name { get; set; }
    
double Price { get; set; }
    
string X_More();
    
string More();
}

public class I_5_L:I_5_A
{
    
private string name;
    
private double price;

    
public I_5_L()
    
{
        name 
= "吴有鋆";
        price 
= 66.888;
    }

    
I_5_A 成员#region I_5_A 成员

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

        
set
        
{
            name 
= value;
        }

    }


    
public double Price
    
{
        
get
        
{
            
return price;
        }

        
set
        
{
            price 
= value;
        }

    }


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


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


    
#endregion

}

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

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

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

 

实例六:

Code
public interface I_6_A
{
    
string GetUrl();
    
string GetName();
}

public class I_6_L_1:I_6_A
{

    
I_6_A 成员#region I_6_A 成员

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


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


    
#endregion

}

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

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


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


    
#endregion

}

public class I_6_L_3 : I_6_L_1
{
    
public override string GetUrl()
    
{
        
return "www.thc123.net";
    }

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

}

public class I_6_L_4 : I_6_L_1
{
    
public new string GetUrl()
    
{
        
return "没地址";
    }

    
public new string GetName()
    
{
        
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=没有名字

 

 实例九:

Code
    public interface I_9_A
    
{
        
int Age { get; set; }
        
string GetName();
        
string GetPwd();
    }

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

        
I_9_A 成员#region I_9_A 成员

        
public int Age
        
{
            
get
            
{
                
return age;
            }

            
set
            
{
                age 
= value;
            }

        }


        
public  string GetName()
        
{
            
return name;
        }


        
public  string GetPwd()
        
{
            
return pwd;
        }


        
#endregion

    }

    
public class I_9_L_2:I_9_L_1
    
{
        
public new string GetName()
        
{
            
return "New方法:" + name;
        }

        
public new string GetPwd()
        
{
            
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方法

Code
    public interface I_9_A
    
{
        
int Age { get; set; }
        
string GetName();
        
string GetPwd();
    }

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

        
I_9_A 成员#region I_9_A 成员

        
public int Age
        
{
            
get
            
{
                
return age;
            }

            
set
            
{
                age 
= value;
            }

        }


        
public virtual string GetName()
        
{
            
return name;
        }


        
public virtual string GetPwd()
        
{
            
return pwd;
        }


        
#endregion

    }

    
public class I_9_L_2:I_9_L_1
    
{
        
public override string GetName()
        
{
            
return "New方法:" + name;
        }

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

    }

    
public class I_9_L_3 : I_9_L_2
    
{
        
public override string GetName()
        
{
            
return "I_9_L_3 New方法" + name;
        }

        
public override string GetPwd()
        
{
            
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

posted @ 2008-10-22 00:41  吴有鋆  阅读(700)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3