前几天又有人不知道接口与抽象类的区别,我回头就做了这个例子。今天发布在我的空间中。
首先我们来解释下抽象类与接口:接口只能有方法签名,属性签名而在抽象里面你可以把设计一个抽象方法同时这个抽象类也支持完整有业务逻辑块的方法,抽象类不能实例化,所以大家注意下如果有方法语句块需要把方法声明成静态的。
最后我来帮大家解释下它们两者之间的什么时候需要使用接口什么时候选择使用抽象类,什么时候选择使用接口。
条件如下:
当一个方法要使用多次的时候就选择使用接口。
当一个方法只需要使用一次的时候选择使用抽象类
接下来我们来看看它们之间的代码。
三个CS文件,一个winform窗体
接口
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace WindowsFormsApplication2
6 {
7 interface ITeacher//跨程序集使用必须使用修饰符public
8 {
9 string Name();
10 int Age();
11 }
12 }
13
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace WindowsFormsApplication2
6 {
7 interface ITeacher//跨程序集使用必须使用修饰符public
8 {
9 string Name();
10 int Age();
11 }
12 }
13
抽象类
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace WindowsFormsApplication2
6 {
7 abstract public class Student//一样必须使用修饰符
8 {
9 public static string Name()
10 {
11 return "XiaoLongZhang";
12 }
13 public static int Age()
14 {
15 return 90;
16 }
17 abstract public string Job();
18 abstract public string Sex();
19 }
20 }
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace WindowsFormsApplication2
6 {
7 abstract public class Student//一样必须使用修饰符
8 {
9 public static string Name()
10 {
11 return "XiaoLongZhang";
12 }
13 public static int Age()
14 {
15 return 90;
16 }
17 abstract public string Job();
18 abstract public string Sex();
19 }
20 }
类继承接口与抽象类。
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace WindowsFormsApplication2
6 {
7 public class Person : Student, ITeacher
8 {
9
10 public override string Job()
11 {
12 return "教师";
13 }
14
15 public override string Sex()
16 {
17 return "男";
18 }
19
20 #region ITeacher 成员
21
22 string ITeacher.Name()//如果是跨程序集使用,必须在接口那修改访问级别(注:在接口类的方法前不要加修饰符)
23 {
24 return "张晓龙";
25 }
26
27 int ITeacher.Age()
28 {
29 return 23;
30 }
31
32 #endregion
33 }
34 }
35
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace WindowsFormsApplication2
6 {
7 public class Person : Student, ITeacher
8 {
9
10 public override string Job()
11 {
12 return "教师";
13 }
14
15 public override string Sex()
16 {
17 return "男";
18 }
19
20 #region ITeacher 成员
21
22 string ITeacher.Name()//如果是跨程序集使用,必须在接口那修改访问级别(注:在接口类的方法前不要加修饰符)
23 {
24 return "张晓龙";
25 }
26
27 int ITeacher.Age()
28 {
29 return 23;
30 }
31
32 #endregion
33 }
34 }
35
WinForm窗体
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace WindowsFormsApplication2
11 {
12 public partial class Test : Form
13 {
14
15 public Test()
16 {
17 InitializeComponent();
18 }
19
20 private void btnAbstract_Click(object sender, EventArgs e)
21 {
22 Person _person = new Person();
23 this.txtAbstractName.Text = Student.Name();
24 this.txtAbstractAge.Text = Convert.ToString(Student.Age());
25 this.txtAbstractJob.Text = _person.Job();
26 this.txtAbstractSex.Text = _person.Sex();
27 }
28
29 private void btnInterface_Click(object sender, EventArgs e)
30 {
31 ITeacher _IMyTeacher = new Person();
32 this.txtInterfaceName.Text = _IMyTeacher.Name();
33 this.txtInterfaceAge.Text = Convert.ToString(_IMyTeacher.Age());
34 }
35 }
36 }
37
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace WindowsFormsApplication2
11 {
12 public partial class Test : Form
13 {
14
15 public Test()
16 {
17 InitializeComponent();
18 }
19
20 private void btnAbstract_Click(object sender, EventArgs e)
21 {
22 Person _person = new Person();
23 this.txtAbstractName.Text = Student.Name();
24 this.txtAbstractAge.Text = Convert.ToString(Student.Age());
25 this.txtAbstractJob.Text = _person.Job();
26 this.txtAbstractSex.Text = _person.Sex();
27 }
28
29 private void btnInterface_Click(object sender, EventArgs e)
30 {
31 ITeacher _IMyTeacher = new Person();
32 this.txtInterfaceName.Text = _IMyTeacher.Name();
33 this.txtInterfaceAge.Text = Convert.ToString(_IMyTeacher.Age());
34 }
35 }
36 }
37