试图了解接口

扫码查看
} HttpContext.Current.Response.Write("< BR>"); } //功能 public void OnSharpened() { m_Sharpened = true; m_message ="机械铅笔是自锐的。; Wri te(); } public void Sharpen(IPencil pencil) { } } }Hi,I have been reading on interfaces working on samples I''ve run across onthe web. For the life of me I cannot seem to grasp them.It appears to me that interfaces are simply blueprints to a class, thatwhen implemented, they require the implementing calss to make sure thateach of the properties, functions etc. are handled by the class. (????)What I am really having a problem with is how they overcome the limitationof multiple inheritance? It would appear that interfaces simply requirethat a class handles a predetermined number of properties events etc. Soif I have a class that implements an interface and I need to addfunctionality to that class...I still have to add it to the interface andhandle it in the initial class...this seems redundant to me. Not tomention modifying the interface to include the new required functionality,now makes all other classes that implement the interface "break" and nowrequire that I modify all classes that implement the interface to handlethe newly added functionality.Now I openly admit I am no OOP guru, and I am simply trying to understandinterfaces...I am certainly not bashing them.The code below illustrates my initial attempt at understandinginterfaces...pieced together from a few tutorials on the web.The classes and intefaces apper below. and the instantiation of theobjects appears at the top...and will probably shed light on my lack ofunderstanding of interfaces.The first instantiation illustrates the use of the interfaces:public partial class _Default : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){IPencil p = new Pencil();p.Type = "Pencil";IPencil mp = new MechanicalPencil();mp.Type = "MechanicalPencil";IPencil pen = new Pen();pen.Type = "Pen";PencilSharpener sharpener = new PencilSharpener();sharpener.Sharpen(p);sharpener.Sharpen(mp);sharpener.Sharpen(pen);}Now where I get confused is that I can quite simply get the same resultsby bypassing the interfaces and never calling the PencilSharpener class:protected void Page_Load(object sender, EventArgs e){IPencil p = new Pencil();p.Type = "Pencil";p.OnSharpened();IPencil mp = new MechanicalPencil();mp.Type = "MechanicalPencil";IPencil pen = new Pen();pen.Type = "Pen";//PencilSharpener sharpener = new PencilSharpener();//sharpener.Sharpen(p);//sharpener.Sharpen(mp);//sharpener.Sharpen(pen);}So this is where my paridigm needs some shifting :-)Can you please help shed some light on this? Thank you very much!Ron// Interfaces +++++++++++++++++++++++++++++++++//Pencil Interfacepublic interface IPencil{string Type { get; set; }int CurrentSharpness { get; set; }bool IsSharp { get; }void Write();void OnSharpened();}//Pencil Sharpener Interfacepublic interface IPencilSharpener{void Sharpen(IPencil pencil);}// Classes +++++++++++++++++++++++++++++++++++++// Pencil Classpublic class Pencil : IPencil{private string m_message;private string m_sharpenedMessage;private string m_type;private int m_currentSharpness;private int m_charsUsed;private Boolean m_Sharpened = false;// Constructorpublic Pencil(){m_type = string.Empty;m_currentSharpness = 0;m_message = string.Empty;m_sharpenedMessage = string.Empty;m_charsUsed = 0;}// Propertypublic string Message{get { return m_message; }}// Propertypublic string SharpenedMessage{get { return m_sharpenedMessage; }}// Propertypublic string Type{get { return m_type; }set {m_type = value;m_message = "This is my " + m_type + " writing away!";m_sharpenedMessage = "This is one sharp " + m_type + "!";}}// Propertypublic int CurrentSharpness{get { return m_currentSharpness; }set { m_currentSharpness = value; }}// Propertypublic bool IsSharp{get { return m_charsUsed <= m_currentSharpness; }}// Functionpublic void Write(){foreach (char c in m_message){if (m_Sharpened == false){if (IsSharp){HttpContext.Current.Response.Write(c);}else{HttpContext.Current.Response.Write("#");}}else{HttpContext.Current.Response.Write(c);}m_charsUsed++;}HttpContext.Current.Response.Write("<BR>");}// Functionpublic void OnSharpened(){while(this.m_currentSharpness < m_message.Length){m_charsUsed = 0;m_currentSharpness++;Write();}m_Sharpened = true;m_message = m_sharpenedMessage;Write();}public void Sharpen(IPencil pencil){pencil.OnSharpened();}}// Pencil Sharpener Classpublic class PencilSharpener : IPencilSharpener{public void Sharpen(IPencil pencil){HttpContext.Current.Response.Write("<br>Begin sharpening " + pencil.Type +"...<br>");pencil.OnSharpened();}}// Pen Classclass Pen : IPencil{private string m_message;private string m_sharpenedMessage;private string m_type;private int m_currentSharpness;private int m_charsUsed;private Boolean m_Sharpened = false;// Constructorpublic Pen(){m_type = string.Empty;m_currentSharpness = 0;m_message = string.Empty;m_sharpenedMessage = string.Empty;m_charsUsed = 0;}// Propertypublic string Message{get { return m_message; }}// Propertypublic string SharpenedMessage{get { return m_sharpenedMessage; }}// Propertypublic string Type{get { return m_type; }set { m_type = value; }}// Propertypublic int CurrentSharpness{get { return m_currentSharpness; }set { m_currentSharpness = 0; }}// Propertypublic bool IsSharp{get { return m_charsUsed <= m_currentSharpness; }}public void Write(){HttpContext.Current.Response.Write(m_message + "<BR>");}// Functionpublic void OnSharpened(){m_Sharpened = true;m_message = "A Pen cannot be sharpened!.";Write();}}// Mechanical Pencil Classclass MechanicalPencil : IPencil, IPencilSharpener{private string m_message;private string m_sharpenedMessage;private string m_type;private int m_currentSharpness;private int m_charsUsed;private Boolean m_Sharpened = false;// Constructorpublic MechanicalPencil(){m_type = string.Empty;m_currentSharpness = 0;m_message = string.Empty;m_sharpenedMessage = string.Empty;m_charsUsed = 0;}// Propertypublic string Message{get { return m_message; }}// Propertypublic string SharpenedMessage{get { return m_sharpenedMessage; }}// Propertypublic string Type{get { return m_type; }set { m_type = value; }}// Propertypublic int CurrentSharpness{get { return m_currentSharpness; }set { m_currentSharpness = value; }}// Propertypublic bool IsSharp{get { return m_charsUsed <= m_currentSharpness; }}// Functionpublic void Write(){foreach (char c in m_message){if (m_Sharpened == false){if (IsSharp){HttpContext.Current.Response.Write(c);}else{HttpContext.Current.Response.Write("#");}}else{HttpContext.Current.Response.Write(c);}m_charsUsed++;}HttpContext.Current.Response.Write("<BR>");}// Functionpublic void OnSharpened(){m_Sharpened = true;m_message = "The Mechanical Pencil is self sharpening.";Write();}public void Sharpen(IPencil pencil){}}}接口还允许您对功能区域进行分组,使得开发更简单。 例如想象一个轮子代表带轮胎的轮子的物体 它。你可能会看到方法和属性,如宽度, 高度,胎面花纹,轮辐样式,阀门,充气,抛光,放气, tyremake,hubmake,tyremanufacturer, hubmanufacturer。 现在,当你对这个轮子进行编程时,你会看到一个很大的属性列表,它可能不是立即的清楚他们是什么。做什么。 现在有一个实现IHub的轮对象和ITyre的东西变得更清楚了。你知道IHub.polish会打磨轮毂,之前你可能会因为无意中抛光了轮胎。您现在也可以删除 tyremanufacturer和hubmanufacturer,只需在每个接口上设置制造商 。Interfaces also allow you to group areas of functionality which makesdeveloping simpler.For example imagine a wheel object representing a wheel with a tyre onit. You might expect to see methods and properties like , width,height, tread pattern, spoke style, valve, inflate, polish, deflate,tyremake, hubmake, tyremanufacturer, hubmanufacturer.Now when you''re programming against that wheel you''re going to see ahuge list of properties and it might not be immediately clear what theydo.Now have a wheel object that implements IHub and ITyre things becomeclearer. you know IHub.polish will polish the hub, previously you mighthave inadvertantly polished the tyre. You can also now removetyremanufacturer and hubmanufacturer and simply have manufacturer oneach of the interfaces.我是否正确,他们自己没有提供真正的直接功能? 相反,他们只是提供一种方法来确保实例化对象 符合预定的规则集。 ...并提供一种机制,用于未来的开发,以及开发人员对类的强制性属性和功能的开发? DeveloperX < nn ***** @ operamail.com写的消息 news:11 ********************* @ i42g2000cwa.googlegro ups.com ...Am I correct in that they provide no real direct functionality themselves?Instead they simply provide a way to ensure that instantiating objectscomply a predetermined set of rules. ...And provide a mechanism for futuredevelopment against the class that enlightens the developers on the class''smandatory properties, and functions?"DeveloperX" <nn*****@operamail.comwrote in messagenews:11*********************@i42g2000cwa.googlegro ups.com... 接口还允许您对功能区域进行分组,使得开发更简单。 例如想象一个轮子对象代表轮胎上的轮子 它。你可能会看到方法和属性,如宽度, 高度,胎面花纹,轮辐样式,阀门,充气,抛光,放气, tyremake,hubmake,tyremanufacturer, hubmanufacturer。 现在,当你对这个轮子进行编程时,你会看到一个很大的属性列表,它可能不是立即的清楚他们是什么。做什么。 现在有一个实现IHub的轮对象和ITyre的东西变得更清楚了。你知道IHub.polish会打磨轮毂,之前你可能会因为无意中抛光了轮胎。您现在也可以删除 tyremanufacturer和hubmanufacturer,只需在每个接口上设置制造商 。Interfaces also allow you to group areas of functionality which makesdeveloping simpler.For example imagine a wheel object representing a wheel with a tyre onit. You might expect to see methods and properties like , width,height, tread pattern, spoke style, valve, inflate, polish, deflate,tyremake, hubmake, tyremanufacturer, hubmanufacturer.Now when you''re programming against that wheel you''re going to see ahuge list of properties and it might not be immediately clear what theydo.Now have a wheel object that implements IHub and ITyre things becomeclearer. you know IHub.polish will polish the hub, previously you mighthave inadvertantly polished the tyre. You can also now removetyremanufacturer and hubmanufacturer and simply have manufacturer oneach of the interfaces. 这篇关于试图了解接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 04:16
查看更多