素材巴巴 > 程序开发 >

#AE之接口查询技术(QI)

程序开发 2023-09-09 06:59:12

概念:接口查询(Query Interface),简称QI,页脚接口跳转。它的实质是指从类的一个接口跳转到其他的接口。

QI是学号AE的前提。

下面通过代码的方式来讲解接口跳转(QI):

隐式实现接口:

1.定义接口IcarA:

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;namespace qitest
 {interface IcarA{void move();}
 } 

2.定义接口IcarB:

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;namespace qitest
 {interface IcarB{void move();}
 } 

3.定义一个隐式实现类car:

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;namespace qitest
 {class car:IcarA, IcarB{public void move(){MessageBox.Show("慢速");}void IcarB.move(){MessageBox.Show("快速");}}
 } 

4.测试:点击button,先后弹出“慢速”,“快速”,再次弹出“慢速”,此时car的类的对象能够调用move方法,并且仅仅能实现IcarA接口的方法。

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;namespace qitest
 {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){IcarA a = new car();a.move();IcarB b = new car();b.move();car c = new car();c.move();}}
 }
  

显示实现接口:

1.将car显示实现接口:此时已经不能使用car类的实例化对象调用接口的方法,只能通过定义接口类型,采用实现类car来实例化接口的对象,来调用相应接口的方法。

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;namespace qitest
 {class car:IcarA, IcarB{void move(){MessageBox.Show("慢速");}void IcarB.move(){MessageBox.Show("快速");}}
 } 

2.测试:此时car类直接的实例化的类对象已经不能调用接口的move()方法了。

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;namespace qitest
 {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){IcarA a = new car();a.move();IcarB b = new car();b.move();car ca = new car();ca.move();}}
 } 

接口跳转QI:

1.定义IArea接口:

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;namespace QI
 {interface IArea{void area();}
 } 

2.定义ILength接口:

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;namespace QI
 {interface ILength{void length();}
 }
  

3.定义显示实现类jisuan:

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;namespace QI
 {class jisuan:ILength, IArea{void ILength.length(){MessageBox.Show("计算长度");}void IArea.area(){MessageBox.Show("计算面积");}}
 }
  

4.测试:此时运行结果就是接口跳转实现的。简单说就是:将一个接口的实现类的实例化对象转到该类的另外一个实现的接口的实例化对象,来实现另外一个接口的方法。

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;namespace QI
 {public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){/*IArea area = new jisuan();ILength len = new jisuan();area.area();len.length();*/IArea js = new jisuan();js.area();ILength len = js as ILength;//接口跳转len.length();}}
 }
  

标签:

上一篇: nginx访问静态资源报404错误的坑 下一篇:
素材巴巴 Copyright © 2013-2021 http://www.sucaibaba.com/. Some Rights Reserved. 备案号:备案中。