图书馆网站建设情况宁波网页设计在宁勤区
web/
2025/9/27 7:23:54/
文章来源:
图书馆网站建设情况,宁波网页设计在宁勤区,视频分享网站怎么做,三亚网址之家本文转自#xff1a;http://blog.***/article.asp?id89介绍#xff1a;在本文中#xff0c;我将借一个DEMO讨论下接口和抽象类。抽象类和接口的的概念对初学面向对象编程的人来说#xff0c;总容易迷惑。所以#xff0c;我试着讨论下两者的理论并比较他们的使用。最后我将…本文转自http://blog.***/article.asp?id89 介绍 在本文中我将借一个DEMO讨论下接口和抽象类。抽象类和接口的的概念对初学面向对象编程的人来说总容易迷惑。所以我试着讨论下两者的 理论并比较他们的使用。最后我将演示下怎么用C#使用它们。 背景 一个抽象类在没有执行时就象一个接口一样。但在它们间有很多不同点下面就解释它们的概念比较他们的相似点和不同点。 什么是抽象类 一个抽象类是一种特殊的类它无法实例化。所以我们会问那我们为什么还需要一个无法实例化的类呢一个抽象类仅用来被继承的即它只 允许其他类继承它但自己不能实例化。对继承的子集抽象类的优势是增强了某些层次结构。简而言之它是一种契约使所有继承它的子类都带有 一样的层次结构或标准。 什么是接口 接口不是一个类。一个接口没有执行机制。它只能有一个信号换句话说它只能够定义方法名字方法怎么实现的一无所有。有一点是和抽象类 相似的它也具有一种契约接口被用来定义所有子类的结构或者说是定义一套子类方法。它们间的主要区别就是一个类能实现或执行多个接口 而只能从一个抽象类继承。在C#中不支持向C那样一个类可以多重继承但接口的运用从另外的角度看以用来实现了多继承。 接口和抽象类 当我们创建一个接口相当于我们基本上创建了一套没有执行的方法需要我们在子类中过载Overriden实现。这样做有一个非常显著的优势 它提供了一种方法使一个类可以成为两个类的一部分. 当我们创建一个抽象类相当于我们创建了一个基类它拥有一个或着多个完整的方法 但至少有一个方法没有实现即被声明为抽象方法。若所有的方法都没实现那它和接口的功能是一样的只是它还遵守无法被继承的规则。 抽象类的目的是提供了一个基本类定义了子类将如何设计允许程序员在子类中实现这些方法。 特点 接 口 抽象类 多继承 一个类能从几个接口继承 一个类只能继承于一个抽象类 实现方式 一个接口没有任何实现的代码只显示方法名 抽象类可以提供完整的方法实现的代码 核心与外围 接口用来定义类的外围功能. 比如人类和交通工具两个类都能从IMOVABLE接口继承。 抽象类多用来定义一个类的核心层 使用场合 如果很多实现都共享一些方法则 用接口比较 如果很多实现使用同一系列方法使用一样的属性则用抽象类较好 速度 要求更多的时间去找到实际的子类实现的方法 比较快 功能扩展性 如果我们要给接口添加一个方法我们要捕捉所有使用该接口的实现子类并分别添加一个新的方法并实现它. 如果我们要给抽象类添加一个新的方法我们可以在抽象实现也可以在子类实现 代码实例 用代码来实现会更简单些。这里有一个Employee抽象类和一个IEmployee接口。我会用实现的对象分别继承于Employee和IEmployeeEmp_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 讲解了他们实现的不同点.转载于:https://www.cnblogs.com/feima-lxl/archive/2008/04/25/1170971.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/81221.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!