试图了解接口 [英] Trying to understand interfaces

查看:71
本文介绍了试图了解接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我一直在阅读我在

网页上运行的样本界面。对于我的生活,我似乎无法掌握它们。


在我看来,接口只是一个类的蓝图,当实现

时,它们需要实现calss以确保每个属性,函数等都由类处理。 (????)


我真正遇到的问题是他们如何克服多重继承的限制

?看来接口只需要

a类处理预定数量的属性事件等。所以如果我有一个实现接口的类,我需要添加功能这个类......我还是要把它添加到接口并在

初始类中处理它...这对我来说似乎是多余的。更不用说修改

界面以包含新的所需功能,现在使所有其他

类实现接口break现在要求我修改

所有实现接口的类来处理新添加的

功能。


现在我公开承认我不是OOP大师,我只是想了解

接口......我当然不会抨击他们。


下面的代码说明了我的最初尝试理解

接口...从网上的一些教程拼凑而成。


下面的类和接口。并且对象的实例化

出现在顶部...并且可能会揭示我对接口的理解不足




第一个实例化说明了接口的使用:

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 = 笔;


PencilSharpener sharpener = new PencilSharpener();


sharpener.Sharpen(p);


sharpener.Sharpen(mp);


sharpener.Sharpen(笔);


}


现在我感到困惑的是,我可以很简单地得到相同的结果s by

绕过接口并且从不调用PencilSharpener类:


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);


}


所以这是我的paridigm需要一些转变的地方:-)


你能帮忙解释一下吗?非常感谢!


Ron



//接口++++++++++++++++++++++++++ +++++++++++++++++++++++++++ +>>>>>> $ $ $ $ $ $ $ $ $
公共接口IPencil


{


string Type {get;组; }


int CurrentSharpness {get;组; }


bool IsSharp {get; }


void写();


void OnSharpened();


}


//卷笔刀界面


公共界面IPencilSharpener


{

void锐化(IPencil铅笔);


}



//类++ +++++++++++++++++++++++++++++++++++++++++++++班级


公共类铅笔:IPencil


{


private string m_message;


私有字符串m_sharpenedMessage;


私有字符串m_type;


private int m_currentSharpness;


private int m_charsUsed;


private Boolean m_Sharpened = false;


//构造函数


公共铅笔()


{


m_type = string.Empty;


m_currentSharpness = 0;


m_message = string.Empty;


m_sharpenedMessage = string.Empty;


m_charsUsed = 0;


}


//财产


公共字符串消息


{


get {return m_message; }


}


//物业


公共字符串SharpenedMessage


{


get {return m_sharpenedMessage; }


}


//物业


公共字符串类型


{


get {return m_type; }


set {


m_type = value;


m_message ="这是我的" + m_type +"写下来!" ;;


m_sharpenedMessage ="这是一个尖锐的 + m_type +"!";


}


}


//物业


public int CurrentSharpness


{


get {return m_currentSharpness; }


set {m_currentSharpness = value; }


}


//物业


公共布尔IsSharp


{


get {return m_charsUsed< = m_currentSharpness; }


}


//功能


public void写()


{


foreach(字符在m_message中)


{


if(m_Sharpened == false)


{


if(IsSharp)


{


HttpContext.Current.Response.Write(c);


}


否则


{


HttpContext.Current.Response.Write(&#;");


}


}


其他


{

HttpContext.Current.Response.Write(c);


}


m_charsUsed ++;

}


HttpContext.Current.Response.Write("< BR>");


}


//功能


public void OnSharpened()


{


while(this.m_currentSharpness< m_message.Length)


{


m_ch arsUsed = 0;


m_currentSharpness ++;


写();


}


m_Sharpened = true;


m_message = m_sharpenedMessage;


写();
< br $>
}


public void Sharpen(IPencil pencil)


{


pencil.OnSharpened();


}


}


//卷笔刀课程


公共类PencilSharpener:IPencilSharpener


{


public void Sharpen(IPencil pencil)


{


HttpContext.Current.Response.Write(" br> Begin sharpening" + pencil.Type +

" ...< br>");


pencil.OnSharpened();


}


}


//笔类


class Pen :IPencil


{


private string m_message;


private string m_sharpenedMessage;


私有字符串m_type;


private int m_currentSharpness;


private int m_charsUsed;


private Boolean m_Sharpened = false;


//构造函数


public Pen()


{


m_type = string.Empty;


m_currentSharpness = 0;


m_message = string.Empty;


m_sharpenedMessage = string.Empty;


m_charsUsed = 0;

}


//物业


公共字符串消息


{


get {return m_message; }


}


//物业


公共字符串SharpenedMessage


{


get {return m_sharpenedMessage; }


}


//物业


公共字符串类型


{


get {return m_type; }


set {m_type = value; }


}


//物业


public int CurrentSharpness


{


get {return m_currentSharpness; }


set {m_currentSharpness = 0; }


}


//物业


公共布尔IsSharp


{


get {return m_charsUsed< = m_currentSharpness; }


}


public void写()


{


HttpContext.Current.Response.Write(m_message +"< BR>");


}


//功能


public void OnSharpened()


{


m_Sharpened = true;


m_message ="笔不能被锐化!。" ;;


写();


}


}


//机械铅笔班


类MechanicalPencil: IPencil,IPencilSharpener


{


private string m_message;


private string m_sharpenedMessage;


私有字符串m_type;


private int m_currentSharpness;


private int m_charsUsed;


private Boolean m_Sharpened = false;


//构造函数


public MechanicalPencil()


{


m_typ e = string.Empty;


m_currentSharpness = 0;


m_message = string.Empty;


m_sharpenedMessage = string.Empty;


m_charsUsed = 0;


}


/ /物业


公共字符串消息


{


get {return m_message; }


}


//物业


公共字符串SharpenedMessage


{


get {return m_sharpenedMessage; }


}


//物业


公共字符串类型


{


get {return m_type; }


set {m_type = value; }


}


//物业


public int CurrentSharpness


{


get {return m_currentSharpness; }


set {m_currentSharpness = value; }


}


//物业


公共布尔IsSharp


{


get {return m_charsUsed< = m_currentSharpness; }


}


//功能


public void写()


{


foreach(字符在m_message中)


{


if(m_Sharpened == false)


{


if(IsSharp)


{


HttpContext.Current.Response.Write(c);


}


否则


{


HttpContext.Current.Response.Write(&#;");


}


}


其他


{

HttpContext.Current.Response.Write(c);


}


m_charsUsed ++;

}


HttpContext.Current.Response.Write("< BR>");


}


//功能


public void OnSharpened()


{


m_Sharpened = true;


m_message ="机械铅笔是自我锐化的g。" ;;


写();


}


public void Sharpen( IPencil铅笔)


{


}


}


}

Hi,

I have been reading on interfaces working on samples I''ve run across on the
web. For the life of me I cannot seem to grasp them.

It appears to me that interfaces are simply blueprints to a class, that when
implemented, they require the implementing calss to make sure that each of
the properties, functions etc. are handled by the class. (????)

What I am really having a problem with is how they overcome the limitation
of multiple inheritance? It would appear that interfaces simply require that
a class handles a predetermined number of properties events etc. So if I
have a class that implements an interface and I need to add functionality to
that class...I still have to add it to the interface and handle it in the
initial class...this seems redundant to me. Not to mention modifying the
interface to include the new required functionality, now makes all other
classes that implement the interface "break" and now require that I modify
all classes that implement the interface to handle the newly added
functionality.

Now I openly admit I am no OOP guru, and I am simply trying to understand
interfaces...I am certainly not bashing them.

The code below illustrates my initial attempt at understanding
interfaces...pieced together from a few tutorials on the web.

The classes and intefaces apper below. and the instantiation of the objects
appears at the top...and will probably shed light on my lack of
understanding 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 results by
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 Interface

public interface IPencil

{

string Type { get; set; }

int CurrentSharpness { get; set; }

bool IsSharp { get; }

void Write();

void OnSharpened();

}

//Pencil Sharpener Interface

public interface IPencilSharpener

{

void Sharpen(IPencil pencil);

}



// Classes +++++++++++++++++++++++++++++++++++++

// Pencil Class

public 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;

// Constructor

public Pencil()

{

m_type = string.Empty;

m_currentSharpness = 0;

m_message = string.Empty;

m_sharpenedMessage = string.Empty;

m_charsUsed = 0;

}

// Property

public string Message

{

get { return m_message; }

}

// Property

public string SharpenedMessage

{

get { return m_sharpenedMessage; }

}

// Property

public 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 + "!";

}

}

// Property

public int CurrentSharpness

{

get { return m_currentSharpness; }

set { m_currentSharpness = value; }

}

// Property

public bool IsSharp

{

get { return m_charsUsed <= m_currentSharpness; }

}

// Function

public 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>");

}

// Function

public 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 Class

public class PencilSharpener : IPencilSharpener

{

public void Sharpen(IPencil pencil)

{

HttpContext.Current.Response.Write("<br>Begin sharpening " + pencil.Type +
"...<br>");

pencil.OnSharpened();

}

}

// Pen Class

class 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;

// Constructor

public Pen()

{

m_type = string.Empty;

m_currentSharpness = 0;

m_message = string.Empty;

m_sharpenedMessage = string.Empty;

m_charsUsed = 0;

}

// Property

public string Message

{

get { return m_message; }

}

// Property

public string SharpenedMessage

{

get { return m_sharpenedMessage; }

}

// Property

public string Type

{

get { return m_type; }

set { m_type = value; }

}

// Property

public int CurrentSharpness

{

get { return m_currentSharpness; }

set { m_currentSharpness = 0; }

}

// Property

public bool IsSharp

{

get { return m_charsUsed <= m_currentSharpness; }

}

public void Write()

{

HttpContext.Current.Response.Write(m_message + "<BR>");

}

// Function

public void OnSharpened()

{

m_Sharpened = true;

m_message = "A Pen cannot be sharpened!.";

Write();

}

}

// Mechanical Pencil Class

class 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;

// Constructor

public MechanicalPencil()

{

m_type = string.Empty;

m_currentSharpness = 0;

m_message = string.Empty;

m_sharpenedMessage = string.Empty;

m_charsUsed = 0;

}

// Property

public string Message

{

get { return m_message; }

}

// Property

public string SharpenedMessage

{

get { return m_sharpenedMessage; }

}

// Property

public string Type

{

get { return m_type; }

set { m_type = value; }

}

// Property

public int CurrentSharpness

{

get { return m_currentSharpness; }

set { m_currentSharpness = value; }

}

// Property

public bool IsSharp

{

get { return m_charsUsed <= m_currentSharpness; }

}

// Function

public 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>");

}

// Function

public void OnSharpened()

{

m_Sharpened = true;

m_message = "The Mechanical Pencil is self sharpening.";

Write();

}

public void Sharpen(IPencil pencil)

{

}

}

}


推荐答案

接口适用于具有相同行为但不同

实现该行为。


例如,Person对象可能以一种方式实现Move方法 - 通过

行走。


Car对象可能以另一种方式实现Move方法 - 通过驾驶。


然而,一个人和一辆车,没有别的共同之处。他们不能共享

共同的祖先。


但他们都可以实现相同的界面。而且有一个实现这个接口的对象的对象集合的人可以在每个对象上调用Move方法

,而不考虑对象是否是Car,a Person ,

或其他东西。因为它们都实现了相同的界面,所以它们都可以移动。


界面不是一个类的蓝图。如果你只有一个实现接口的类

,那么你就没有接口。如果

你有多个类实现接口,但是它们都具有相同的实现,那么你应该使用继承。


RSH < wa ************* @ yahoo.comwrote in message

新闻:OX **************** @ TK2MSFTNGP02.phx.gbl ...
Interfaces are meant for classes that have the same behavior, but different
implementation for that behavior.

For example, a Person object might implement a Move method one way - by
walking.

A Car object might implement a Move method another way - by driving.

However, a Person and a Car, have nothing else in common. They can''t share a
common ancestor.

But they can both implement the same interface. And someone with a
collection of objects implementing this interface, can call the Move method
on each of them, without regard as to whether the object is a Car, a Person,
or something else. Because they all implement the same interface, they can
all be moved.

An interface is not a blueprint for a class. If you have only one class
implementing the interface, then you shouldn''t have an interface. And if
you have multiple classes implement the interface, but they all have the
same implementation, then you should be using inheritence.

"RSH" <wa*************@yahoo.comwrote in message
news:OX****************@TK2MSFTNGP02.phx.gbl...




我一直在阅读处理样本的界面我''在网上浏览了

。对于我的生活,我似乎无法掌握它们。


在我看来,接口只是一个类的蓝图,

实施后,它们需要实现calss以确保每个属性,函数等由类处理。 (????)


我真正遇到的问题是他们如何克服多重继承的限制

?看来接口只需要一个类来处理预定数量的属性事件等等。

如果我有一个实现接口的类,我需要添加

该类的功能......我仍然需要将它添加到界面中并且

在初始类中处理它...这对我来说似乎是多余的。不要提及修改界面以包含新的所需功能,

现在使所有其他实现界面的类破解现在

要求我修改所有实现接口的类来处理

新添加的功能。


现在我公开承认我不是OOP大师,我只是想了解

接口......我当然不会抨击他们。


下面的代码说明了我的最初尝试理解

接口...从网上的一些教程拼凑而成。


下面的类和接口。并且

对象的实例化出现在顶部...并且可能会让我了解我对接口的理解不足




第一个实例化说明了接口的使用:

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 = 笔;


PencilSharpener sharpener = new PencilSharpener();


sharpener.Sharpen(p);


sharpener.Sharpen(mp);


sharpener.Sharpen(笔);


}


现在我感到困惑的是,我可以很简单地得到相同的结果

by绕过接口,从不调用PencilSharpener类:


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);


}


所以这就是我的paridigm需要一些转变:-)


你能帮忙解释一下吗?非常感谢!


Ron



//接口+++++++++++++ ++++++++++++++++++++++++++++++++ $ b公共接口IPencil


{


string Type {get;组; }


int CurrentSharpness {get;组; }


bool IsSharp {get; }


void写();


void OnSharpened();


}


//卷笔刀界面


公共界面IPencilSharpener


{

void锐化(IPencil铅笔);


}



//类++ +++++++++++++++++++++++++++++++++++++++++++++班级


公共类铅笔:IPencil


{


private string m_message;


私有字符串m_sharpenedMessage;


私有字符串m_type;


private int m_currentSharpness;


private int m_charsUsed;


private Boolean m_Sharpened = false;


//构造函数


公共铅笔()


{


m_type = string.Empty;


m_currentSharpness = 0;


m_message = string.Empty;


m_sharpenedMessage = string.Empty;

m_charsUsed = 0;


}


//物业


公开字符串消息


{


get {return m_message; }


}


//物业


公共字符串SharpenedMessage


{


get {return m_sharpenedMessage; }


}


//物业


公共字符串类型


{


get {return m_type; }


set {


m_type = value;


m_message ="这是我的" + m_type +"写下来!" ;;


m_sharpenedMessage ="这是一个尖锐的 + m_type +"!";


}


}


//物业


public int CurrentSharpness


{


get {return m_currentSharpness; }


set {m_currentSharpness = value; }


}


//物业


公共布尔IsSharp


{


get {return m_charsUsed< = m_currentSharpness; }


}


//功能


public void写()


{


foreach(字符在m_message中)


{


if(m_Sharpened == false)


{


if(IsSharp)


{


HttpContext.Current.Response.Write(c);


}


否则


{


HttpContext.Current.Response.Write(&#;");


}


}


其他


{

HttpContext.Current.Response.Write(c);


}


m_charsUsed ++;

}


HttpContext.Current.Response.Write("< BR>");


}


//功能


public void OnSharpened()


{


while(this.m_currentSharpness< m_message.Length)


{


m_charsUsed = 0;


m _currentSharpness ++;


写();


}


m_Sharpened = true;


m_message = m_sharpenedMessage;


写();


}


public void Sharpen(IPencil pencil)


{


pencil.OnSharpened();


}


}


//卷笔刀类


公共类PencilSharpener :IPencilSharpener


{


public void Sharpen(IPencil pencil)


{


HttpContext.Current.Response.Write(< br>开始锐化" + pencil.Type +

" ...< br>");


pencil.OnSharpened();


}


}


//笔类


class Pen :IPencil


{


private string m_message;


private string m_sharpenedMessage;


私有字符串m_type;


private int m_currentSharpness;


private int m_charsUsed;


private Boolean m_Sharpened = false;


//构造函数


public Pen()


{


m_type = string.Empty;


m_currentSharpness = 0;


m_message = string.Empty;


m_sharpenedMessage = string.Empty;


m_charsUsed = 0;

}


//物业


公共字符串消息


{


get {return m_message; }


}


//物业


公共字符串SharpenedMessage


{


get {return m_sharpenedMessage; }


}


//物业


公共字符串类型


{


get {return m_type; }


set {m_type = value; }


}


//物业


public int CurrentSharpness


{


get {return m_currentSharpness; }


set {m_currentSharpness = 0; }


}


//物业


公共布尔IsSharp


{


get {return m_charsUsed< = m_currentSharpness; }


}


public void写()


{


HttpContext.Current.Response.Write(m_message +"< BR>");


}


//功能


public void OnSharpened()


{


m_Sharpened = true;


m_message ="笔不能被锐化!。" ;;


写();


}


}



//机械铅笔班


class MechanicalPencil:IPencil,IPencilSharpener

{


private string m_message;


private string m_sharpenedMessage;


私有字符串m_type;


private int m_currentSharpness;


private int m_charsUsed;


private Boolean m_Sharpened = false;


//构造函数


public MechanicalPencil()


{


m_type = string.Empty;

m_currentSharpness = 0;


m_message = string.Empty;


m_sharpenedMessage = string.Empty ;


m_charsUsed = 0;


}


//物业


公共字符串消息


{


get {return m_message; }


}


//物业


公共字符串SharpenedMessage


{


get {return m_sharpenedMessage; }


}


//物业


公共字符串类型


{


get {return m_type; }


set {m_type = value; }


}


//物业


public int CurrentSharpness


{


get {return m_currentSharpness; }


set {m_currentSharpness = value; }


}


//物业


公共布尔IsSharp


{


get {return m_charsUsed< = m_currentSharpness; }


}


//功能


public void写()


{


foreach(字符在m_message中)


{


if(m_Sharpened == false)


{


if(IsSharp)


{


HttpContext.Current.Response.Write(c);


}


否则


{


HttpContext.Current.Response.Write(&#;");


}


}


其他


{

HttpContext.Current.Response.Write(c);


}


m_charsUsed ++;

}


HttpContext.Current.Response.Write("< BR>");


}


//功能


public void OnSharpened()


{


m_Sharpened = true;


m_message ="机械铅笔是自锐的。;


Wri te();


}


public void Sharpen(IPencil pencil)


{< br $>

}


}


}

Hi,

I have been reading on interfaces working on samples I''ve run across on
the web. For the life of me I cannot seem to grasp them.

It appears to me that interfaces are simply blueprints to a class, that
when implemented, they require the implementing calss to make sure that
each of the properties, functions etc. are handled by the class. (????)

What I am really having a problem with is how they overcome the limitation
of multiple inheritance? It would appear that interfaces simply require
that a class handles a predetermined number of properties events etc. So
if I have a class that implements an interface and I need to add
functionality to that class...I still have to add it to the interface and
handle it in the initial class...this seems redundant to me. Not to
mention modifying the interface to include the new required functionality,
now makes all other classes that implement the interface "break" and now
require that I modify all classes that implement the interface to handle
the newly added functionality.

Now I openly admit I am no OOP guru, and I am simply trying to understand
interfaces...I am certainly not bashing them.

The code below illustrates my initial attempt at understanding
interfaces...pieced together from a few tutorials on the web.

The classes and intefaces apper below. and the instantiation of the
objects appears at the top...and will probably shed light on my lack of
understanding 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 results
by 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 Interface

public interface IPencil

{

string Type { get; set; }

int CurrentSharpness { get; set; }

bool IsSharp { get; }

void Write();

void OnSharpened();

}

//Pencil Sharpener Interface

public interface IPencilSharpener

{

void Sharpen(IPencil pencil);

}



// Classes +++++++++++++++++++++++++++++++++++++

// Pencil Class

public 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;

// Constructor

public Pencil()

{

m_type = string.Empty;

m_currentSharpness = 0;

m_message = string.Empty;

m_sharpenedMessage = string.Empty;

m_charsUsed = 0;

}

// Property

public string Message

{

get { return m_message; }

}

// Property

public string SharpenedMessage

{

get { return m_sharpenedMessage; }

}

// Property

public 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 + "!";

}

}

// Property

public int CurrentSharpness

{

get { return m_currentSharpness; }

set { m_currentSharpness = value; }

}

// Property

public bool IsSharp

{

get { return m_charsUsed <= m_currentSharpness; }

}

// Function

public 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>");

}

// Function

public 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 Class

public class PencilSharpener : IPencilSharpener

{

public void Sharpen(IPencil pencil)

{

HttpContext.Current.Response.Write("<br>Begin sharpening " + pencil.Type +
"...<br>");

pencil.OnSharpened();

}

}

// Pen Class

class 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;

// Constructor

public Pen()

{

m_type = string.Empty;

m_currentSharpness = 0;

m_message = string.Empty;

m_sharpenedMessage = string.Empty;

m_charsUsed = 0;

}

// Property

public string Message

{

get { return m_message; }

}

// Property

public string SharpenedMessage

{

get { return m_sharpenedMessage; }

}

// Property

public string Type

{

get { return m_type; }

set { m_type = value; }

}

// Property

public int CurrentSharpness

{

get { return m_currentSharpness; }

set { m_currentSharpness = 0; }

}

// Property

public bool IsSharp

{

get { return m_charsUsed <= m_currentSharpness; }

}

public void Write()

{

HttpContext.Current.Response.Write(m_message + "<BR>");

}

// Function

public void OnSharpened()

{

m_Sharpened = true;

m_message = "A Pen cannot be sharpened!.";

Write();

}

}

// Mechanical Pencil Class

class 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;

// Constructor

public MechanicalPencil()

{

m_type = string.Empty;

m_currentSharpness = 0;

m_message = string.Empty;

m_sharpenedMessage = string.Empty;

m_charsUsed = 0;

}

// Property

public string Message

{

get { return m_message; }

}

// Property

public string SharpenedMessage

{

get { return m_sharpenedMessage; }

}

// Property

public string Type

{

get { return m_type; }

set { m_type = value; }

}

// Property

public int CurrentSharpness

{

get { return m_currentSharpness; }

set { m_currentSharpness = value; }

}

// Property

public bool IsSharp

{

get { return m_charsUsed <= m_currentSharpness; }

}

// Function

public 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>");

}

// Function

public 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 makes
developing simpler.
For example imagine a wheel object representing a wheel with a tyre on
it. 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 a
huge list of properties and it might not be immediately clear what they
do.
Now have a wheel object that implements IHub and ITyre things become
clearer. you know IHub.polish will polish the hub, previously you might
have inadvertantly polished the tyre. You can also now remove
tyremanufacturer and hubmanufacturer and simply have manufacturer on
each 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 objects
comply a predetermined set of rules. ...And provide a mechanism for future
development against the class that enlightens the developers on the class''s
mandatory properties, and functions?
"DeveloperX" <nn*****@operamail.comwrote in message
news: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 makes
developing simpler.
For example imagine a wheel object representing a wheel with a tyre on
it. 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 a
huge list of properties and it might not be immediately clear what they
do.
Now have a wheel object that implements IHub and ITyre things become
clearer. you know IHub.polish will polish the hub, previously you might
have inadvertantly polished the tyre. You can also now remove
tyremanufacturer and hubmanufacturer and simply have manufacturer on
each of the interfaces.



这篇关于试图了解接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆