min10

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

            接口 VS 抽象类


介绍:

   在本文中,我将借一个DEMO讨论下接口和抽象类。抽象类和接口的的概念对初学面向对象编程的人来说,总容易迷惑。所以,我试着讨论下两者的
理论并比较他们的使用。最后我将演示下怎么用
C#使用它们。

背景:

一个抽象类在没有执行时就象一个接口一样。但在它们间有很多不同点,下面就解释它们的概念,比较他们的相似点和不同点。

什么是抽象类?

一个抽象类是一种特殊的类,它无法实例化。所以,我们会问,那我们为什么还需要一个无法实例化的类呢?一个抽象类仅用来被继承的,即它只
允许其他类继承它,但自己不能实例化。对继承的子集,抽象类的优势是增强了某些层次结构。简而言之,它是一种契约,使所有继承它的子类都带有
一样的层次结构或标准。

什么是接口?

接口不是一个类。一个接口没有执行机制。它只能有一个信号,换句话说,它只能够定义方法名字,方法怎么实现的一无所有。有一点是和抽象类
相似的,它也具有一种契约,接口被用来定义所有子类的结构,或者说是定义一套子类方法。它们间的主要区别就是,一个类能实现或执行多个接口,
而只能从一个抽象类继承。在
C#中,不支持向C++那样一个类可以多重继承,但接口的运用,从另外的角度看,以用来实现了多继承。

接口和抽象类:

   当我们创建一个接口,相当于我们基本上创建了一套没有执行的方法,需要我们在子类中过载(Overriden)实现。这样做有一个非常显著的优势,
它提供了一种方法,使一个类可以成为两个类的一部分
.
   当我们创建一个抽象类,相当于我们创建了一个基类,它拥有一个或着多个完整的方法, 
   但至少有一个方法没有实现,即被声明为抽象方法。若所有的方法都没实现,那它和接口的功能是一样的,只是它还遵守无法被继承的规则。
   抽象类的目的是提供了一个基本类,定义了子类将如何设计,允许程序员在子类中实现这些方法。


 

 

       

多继承

一个类能从几个接口继承

一个类只能继承于一个抽象类            

实现方式

一个接口没有任何实现的代码只显示方法名

抽象类可以提供完整的方法实现的代码

核心与外围

接口用来定义类的外围功能.
比如人类和交通工具两个类都能从IMOVABLE接口继承。  

抽象类多用来定义一个类的核心层

使用场合

如果很多实现都共享一些方法,则     

用接口比较

如果很多实现使用同一系列方法,使用一样的属性,则用抽象类较好

速度  

要求更多的时间,去找到实际的子类实现的方法

比较快

功能扩展性

如果我们要给接口添加一个方法,我们要捕捉所有使用该接口的实现子类,并分别添加一个新的方法,并实现它.

如果我们要给抽象类添加一个新的方法我们可以在抽象实现,也可以在子类实现


代码实例:

用代码来实现会更简单些。这里有一个Employee抽象类,和一个IEmployee接口。我会用实现的对象分别继承于EmployeeIEmployeeEmp_Fulltime继承于EmployeeEmp_Fulltime2继承于IEmployee

在测试中,我创建两者的实例,并设置他们的属性,调用一个calculateWage的方法。

Abstract Class Employee

 1 using System;
 2 
 3 namespace AbstractsANDInterfaces
 4 {
 5     /// 
 6 
 7     /// Summary description for Employee.
 8 
 9     /// 
10 
11     
12     public abstract class Employee
13     {
14         //we can have fields and properties 
15 
16         //in the Abstract class
17 
18         protected String id;
19         protected String lname;
20         protected String fname;
21 
22         //properties
23 
24         public abstract String ID
25         {
26             get;
27             set;
28         }
29 
30         public abstract String FirstName
31         {
32             get;
33             set;
34         }
35         
36         public abstract String LastName
37         {
38             get;
39             set;
40         }
41         //completed methods
42 
43         public String Update()
44         {
45             return "Employee " + id + " " + 
46                       lname + " " + fname + 
47                       " updated";
48         }
49         //completed methods
50 
51         public String Add()
52         {
53             return "Employee " + id + " " + 
54                       lname + " " + fname + 
55                       " added";
56         }
57         //completed methods
58 
59         public String Delete()
60         {
61             return "Employee " + id + " " + 
62                       lname + " " + fname + 
63                       " deleted";
64         }
65         //completed methods
66 
67         public String Search()
68         {
69             return "Employee " + id + " " + 
70                       lname + " " + fname + 
71                       " found";
72         }
73 
74         //abstract method that is different 
75 
76         //from Fulltime and Contractor
77 
78         //therefore i keep it uncompleted and 
79 
80         //let each implementation 
81 
82         //complete it the way they calculate the wage.
83 
84         public abstract String CalculateWage();
85         
86     }
87 }
88 

 

 

Interface Employee

 1 using System;
 2 
 3 
 4 namespace AbstractsANDInterfaces
 5 {
 6     /// <summary>
 7 
 8     /// Summary description for IEmployee.
 9 
10     /// </summary>
11 
12     public interface IEmployee
13     {
14         //cannot have fields. uncommenting 
15 
16         //will raise error!
17 
18 //        protected String id;
19 
20 //        protected String lname;
21 
22 //        protected String fname;
23 
24 
25         //just signature of the properties 
26 
27         //and methods.
28 
29         //setting a rule or contract to be 
30 
31         //followed by implementations.
32 
33         String ID
34         {
35             get;
36             set;
37         }
38 
39         String FirstName
40         {
41             get;
42             set;
43         }
44         
45         String LastName
46         {
47             get;
48             set;
49         }
50         
51         // cannot have implementation
52 
53         // cannot have modifiers public 
54 
55         // etc all are assumed public
56 
57         // cannot have virtual
58 
59 
60         String Update();
61 
62         String Add();
63 
64         String Delete();
65 
66         String Search();
67 
68         String CalculateWage();
69     }
70 }
71 

 

 

 

Inherited Objects

Emp_Fulltime:

  1 using System;
  2 
  3 namespace AbstractsANDInterfaces
  4 {
  5     /// 
  6 
  7     /// Summary description for Emp_Fulltime.
  8 
  9     /// 
 10 
 11      
 12     //Inheriting from the Abstract class
 13 
 14     public class Emp_Fulltime : Employee
 15     {
 16         //uses all the properties of the 
 17 
 18         //Abstract class therefore no 
 19 
 20         //properties or fields here!
 21 
 22 
 23         public Emp_Fulltime()
 24         {
 25         }
 26 
 27 
 28         public override String ID
 29         {
 30             get
 31             {
 32                 return id;
 33             }
 34             set
 35             {
 36                 id = value;
 37             }
 38         }
 39         
 40         public override String FirstName
 41         {
 42             get
 43             {
 44                 return fname;
 45             }
 46             set
 47             {
 48                 fname = value;
 49             }
 50         }
 51 
 52         public override String LastName
 53         {
 54             get
 55             {
 56                 return lname;
 57             }
 58             set
 59             {
 60                 lname = value;
 61             }
 62         }
 63 
 64         //common methods that are 
 65 
 66         //implemented in the abstract class
 67 
 68         public new String Add()
 69         {
 70             return base.Add();
 71         }
 72         //common methods that are implemented 
 73 
 74         //in the abstract class
 75 
 76         public new String Delete()
 77         {
 78             return base.Delete();
 79         }
 80         //common methods that are implemented 
 81 
 82         //in the abstract class
 83 
 84         public new String Search()
 85         {
 86             return base.Search();
 87         }
 88         //common methods that are implemented 
 89 
 90         //in the abstract class
 91 
 92         public new String Update()
 93         {
 94             return base.Update();
 95         }
 96         
 97         //abstract method that is different 
 98 
 99         //from Fulltime and Contractor
100 
101         //therefore I override it here.
102 
103         public override String CalculateWage()
104         {
105             return "Full time employee " + 
106                   base.fname + " is calculated " + 
107                   "using the Abstract class";
108         }
109     }
110 }
111 



Emp_Fulltime2

 

  1 using System;
  2 
  3 namespace AbstractsANDInterfaces
  4 {
  5     /// 
  6 
  7     /// Summary description for Emp_fulltime2.
  8 
  9     /// 
 10 
 11     
 12     //Implementing the interface
 13 
 14     public class Emp_fulltime2 : IEmployee
 15     {
 16         //All the properties and 
 17 
 18         //fields are defined here!
 19 
 20         protected String id;
 21         protected String lname;
 22         protected String fname;
 23 
 24         public Emp_fulltime2()
 25         {
 26             //
 27 
 28             // TODO: Add constructor logic here
 29 
 30             //
 31 
 32         }
 33 
 34         public String ID
 35         {
 36             get
 37             {
 38                 return id;
 39             }
 40             set
 41             {
 42                 id = value;
 43             }
 44         }
 45         
 46         public String FirstName
 47         {
 48             get
 49             {
 50                 return fname;
 51             }
 52             set
 53             {
 54                 fname = value;
 55             }
 56         }
 57 
 58         public String LastName
 59         {
 60             get
 61             {
 62                 return lname;
 63             }
 64             set
 65             {
 66                 lname = value;
 67             }
 68         }
 69 
 70         //all the manipulations including Add,Delete, 
 71 
 72         //Search, Update, Calculate are done
 73 
 74         //within the object as there are not 
 75 
 76         //implementation in the Interface entity.
 77 
 78         public String Add()
 79         {
 80             return "Fulltime Employee " + 
 81                           fname + " added.";
 82         }
 83 
 84         public String Delete()
 85         {
 86             return "Fulltime Employee " + 
 87                         fname + " deleted.";
 88         }
 89 
 90         public String Search()
 91         {
 92             return "Fulltime Employee " + 
 93                        fname + " searched.";
 94         }
 95 
 96         public String Update()
 97         {
 98             return "Fulltime Employee " + 
 99                         fname + " updated.";
100         }
101         
102         //if you change to Calculatewage(). 
103 
104         //Just small 'w' it will raise 
105 
106         //error as in interface
107 
108         //it is CalculateWage() with capital 'W'.
109 
110         public String CalculateWage()
111         {
112             return "Full time employee " + 
113                   fname + " caluculated using " + 
114                   "Interface.";
115         }
116     }
117 }
118 

 

 

Code for testing

 1 private void InterfaceExample_Click(object sender, 
 2                                 System.EventArgs e)
 3 {
 4     try
 5     {
 6 
 7         IEmployee emp;
 8 
 9         Emp_fulltime2 emp1 = new Emp_fulltime2();
10         //has to be casted because of the interface!
11 
12         emp = (IEmployee) emp1;
13         emp.ID = "2234";
14         emp.FirstName= "Rahman" ;
15         emp.LastName = "Mahmoodi" ;
16         //call add method od the object
17 
18         MessageBox.Show(emp.Add().ToString());
19         
20         //call the CalculateWage method
21 
22         MessageBox.Show(emp.CalculateWage().ToString());
23 
24 
25     }
26     catch(Exception ex)
27     {
28         MessageBox.Show(ex.Message);
29     }
30 
31 }
32 
33 private void cmdAbstractExample_Click(object sender, 
34                                    System.EventArgs e)
35 {
36 
37     Employee emp;
38     //no casting is requird!
39 
40     emp = new Emp_Fulltime();
41     
42 
43     emp.ID = "2244";
44     emp.FirstName= "Maria" ;
45     emp.LastName = "Robinlius" ;
46     MessageBox.Show(emp.Add().ToString());
47 
48     //call the CalculateWage method
49 
50     MessageBox.Show(emp.CalculateWage().ToString());
51 
52 }
53 

 

结论:

我已经解释了接口和抽象类的不同点,并用一个Demo Project 讲解了他们实现的不同点.

 

注:本文是我花了半天时间,翻译自Rahman Mahmoodi<<Abstract class versus Interface>>.
 

原文链接:http://www.cnblogs.com/jerrymu/archive/2007/12/14/995541.html

posted on 2008-10-24 15:27  min10  阅读(136)  评论(0编辑  收藏  举报