OOP问题 [英] OOP Question

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

问题描述

我正在阅读有关OOP校长和设计的教程,我偶然发现了一些我不理解的东西。该教程使用了Oven类。在那个

类中有一个名为temperature的私人字段。


class Oven {

private int _temperature;


public int温度{

得到{

返回_temperature;

}

设置{

_temperature = value;

}

}

}


这是我通常会采用的实现方式。但是......文章

说...

类型烤箱暴露其温度作为财产,这是直接的

引用变量_temperature。这意味着内部烤箱

温度实现直接与其外部接口相关联。

点好的面向对象编程就是为了避免这种类型的b
编程。


这似乎很奇怪我...如果我必须暴露温度怎么办?如果我的

cookie对象需要将烤箱预热到350度我不需要

监控烤箱对象的温度怎么办?


该死的就在我开始认为我得到OOP的时候!


谢谢,


Ron

I was reading a tutorial on OOP principals and design and I stumbled on
something that I don''t understand. The tutorial used a Oven class. In that
class there was a private field called temperature.

class Oven {
private int _temperature;

public int Temperature {
get {
return _temperature;
}
set {
_temperature = value;
}
}
}

This is the implementation I would normally employ. BUT... The article
says...
"The type Oven exposes its temperature as a property, which is a direct
reference to the variable _temperature. This means that the internal oven
temperature implementation is tied directly to its external interface. The
point of good object-oriented programming is to avoid this sort of
programming."

That seems strange to me...what if I have to expose temperature? What if my
cookie object needs to preheat the oven to 350 degrees dont I need to
monitor the temperature of the oven object?

Damn just when I started to think I was getting OOP!

Thanks,

Ron

推荐答案

12月7日下午1:32,RSH < way_beyond_o ... @ yahoo.comwrote:
On Dec 7, 1:32 pm, "RSH" <way_beyond_o...@yahoo.comwrote:

我正在阅读有关OOP校长和设计的教程,我偶然发现了

的东西我不明白该教程使用了Oven类。在那个

类中有一个名为temperature的私有字段。
I was reading a tutorial on OOP principals and design and I stumbled on
something that I don''t understand. The tutorial used a Oven class. In that
class there was a private field called temperature.



< snip>

<snip>


这是我通常会采用的实现。但是......文章

说...

类型烤箱暴露其温度作为财产,这是直接的

引用变量_temperature。这意味着内部烤箱

温度实现直接与其外部接口相关联。

点好的面向对象编程就是为了避免这种编程的b

This is the implementation I would normally employ. BUT... The article
says...
"The type Oven exposes its temperature as a property, which is a direct
reference to the variable _temperature. This means that the internal oven
temperature implementation is tied directly to its external interface. The
point of good object-oriented programming is to avoid this sort of
programming."



这对我来说听起来不错。实现将始终与界面绑定 - 这是为了避免* reverse *为
为真,即将实现本身暴露为接口。


我当然听说有人说房产应该几乎总是要避免等等,但这并不是一个特别常见的观点。


Jon

That sounds like a bad book to me. The implementation will always be
tied to the interface - the point is to avoid the *reverse* being
true, i.e. exposing the implementation itself as the interface.

I''ve certainly heard people saying that properties should almost
always be avoided etc, but it''s not a particularly commonly-held view.

Jon


Ron,


我很好奇,文章推荐的是解决方法吗?我认为它想要说的是你会有一个温度

对象,你会暴露它会揭露所有工作细节

有温度。


当然,最终,一个人会达到收益递减点,

意味着你只能延伸到目前为止它变得不可能

可以使用(暴露物体的温度,暴露物体为
度等等)。


哦,饼干不会改变温度,Baker实例

会。 Baker实例会将Cookie添加到工作表中,然后将工作表添加到烤箱中。然后在烤箱上调用Bake方法,

这会随着时间的推移改变

单个Cookie实例的Baked属性。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


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

news:e9 ************** @ TK2MSFTNGP02。 phx.gbl ...
Ron,

I am curious, what does the article recommend is the workaround? I
think that what it is trying to say is that you would have a Temperature
object which you would expose which would expose all the details of working
with temperature.

Of course, eventually, one will reach the point of diminishing returns,
meaning that you could only extend this so far before it becomes impossible
to work with (expose an object for temperature, which exposes an object for
degrees, etc, etc).

Oh, and the cookie wouldn''t change the temperature, the Baker instance
would. The Baker instance would add the Cookie to the Sheet, and then add
the Sheet to the Oven. Then the Bake method would be called on the Oven,
which would change the Baked property of the individual Cookie instances on
the sheet over time.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

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

>我正在阅读有关OOP校长和设计的教程,我偶然发现了一些我不明白的东西。该教程使用了Oven类。在那个课堂上有一个叫做温度的私人场地。


班级烤箱{

private int _temperature;


public int Temperature {

get {

return _temperature;

}

set {

_temperature = value;

}

}

}


这个是我通常会采用的实施方式。但是......文章

说...

类型烤箱暴露其温度作为财产,这是直接的

引用变量_temperature。这意味着内部烤箱

温度实现直接与其外部接口相关联。

点好的面向对象编程就是为了避免这种类型的b
编程。


这似乎很奇怪我...如果我必须暴露温度怎么办?如果

我的cookie对象需要将烤箱预热到350度我不需要

监控烤箱对象的温度吗?


该死的就在我开始认为我得到OOP的时候!


谢谢,


Ron

>I was reading a tutorial on OOP principals and design and I stumbled on
something that I don''t understand. The tutorial used a Oven class. In
that class there was a private field called temperature.

class Oven {
private int _temperature;

public int Temperature {
get {
return _temperature;
}
set {
_temperature = value;
}
}
}

This is the implementation I would normally employ. BUT... The article
says...
"The type Oven exposes its temperature as a property, which is a direct
reference to the variable _temperature. This means that the internal oven
temperature implementation is tied directly to its external interface. The
point of good object-oriented programming is to avoid this sort of
programming."

That seems strange to me...what if I have to expose temperature? What if
my cookie object needs to preheat the oven to 350 degrees dont I need to
monitor the temperature of the oven object?

Damn just when I started to think I was getting OOP!

Thanks,

Ron


Nicholas和Jon,


我很欣赏你们的投入......我非常尊重你们两个人并且拥有

从你们两个人那里学到了很多东西。


这是他们建议的解决方案:


委托void OnTemperature(int温度);


类烤箱{

private int _temperature;

OnTemperature _listeners;


public void BroadcastTemperature(){

_listeners(_temperature);

}

public void AddTemperatureListener(OnTemperature listener){

_listeners + =听众;

}

}

类控制器{

公共控制器(奥维烤箱){

oven.AddTemperatureListener(

new OnTemperature(this.OnTemperature));

}

public void OnTemperature(int temperature){

Console.WriteLine(" Temperature(") +温度+")");

}

} http://en.csharp-online.net/CSharp_Coding_Solutions%E2%80%94Dont_Expose_a_Clas s_Internal_StateRon" Nicholas

Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com写信息

新闻:10 ************************* ********* @ microsof t.com ...
Nicholas and Jon,

I appreciate both of your input...I highly respect the both of you and have
learned alot from you both.

This is the solution that they suggest:

delegate void OnTemperature( int temperature);

class Oven {
private int _temperature;
OnTemperature _listeners;

public void BroadcastTemperature() {
_listeners( _temperature);
}
public void AddTemperatureListener( OnTemperature listener) {
_listeners += listener;
}
}
class Controller {
public Controller( Oven oven) {
oven.AddTemperatureListener(
new OnTemperature( this.OnTemperature));
}
public void OnTemperature( int temperature) {
Console.WriteLine( "Temperature (" + temperature + ")");
}
}http://en.csharp-online.net/CSharp_Coding_Solutions%E2%80%94Dont_Expose_a_Clas s_Internal_StateRon"Nicholas
Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in message
news:10**********************************@microsof t.com...

Ron,


我很好奇,该文章推荐的是解决方法?我认为它想要说的是你会有一个温度

对象,你会暴露它会暴露所有细节

使用温度。


当然,最终,一个人将达到收益递减点,

意味着你只能在此之前扩展这个它变得无法使用(暴露一个物体的温度,暴露了一个物体的对象等等),等等。)


哦,饼干不会改变温度,Baker实例

会。 Baker实例会将Cookie添加到工作表中,然后将工作表添加到烤箱中。然后在烤箱上调用Bake方法,

随着时间的推移会改变单个Cookie实例的Baked属性




-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


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

news:e9 ************** @ TK2MSFTNGP02。 phx.gbl ...
Ron,

I am curious, what does the article recommend is the workaround? I
think that what it is trying to say is that you would have a Temperature
object which you would expose which would expose all the details of
working with temperature.

Of course, eventually, one will reach the point of diminishing returns,
meaning that you could only extend this so far before it becomes
impossible to work with (expose an object for temperature, which exposes
an object for degrees, etc, etc).

Oh, and the cookie wouldn''t change the temperature, the Baker instance
would. The Baker instance would add the Cookie to the Sheet, and then add
the Sheet to the Oven. Then the Bake method would be called on the Oven,
which would change the Baked property of the individual Cookie instances
on the sheet over time.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

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

>>我正在阅读关于OOP校长和设计的教程,我偶然发现了一些我不喜欢的事情。明白。该教程使用了Oven类。在那个课堂上有一个叫做温度的私人场地。

类烤箱{
private int _temperature;

public int Temperature {
得到{
返回_temperature;
}
设置{
_temperature = value;
}
}
}
这是我通常会采用的实施方式。但......文章
说......
类型烤箱将其温度暴露为属性,这是对变量_temperature的直接引用。这意味着内部烤箱的温度实现与其外部接口直接相关。
良好的面向对象编程的目的是避免这种编程。

这对我来说很奇怪......如果我必须暴露温度怎么办?如果
我的cookie对象需要将烤箱预热到350度,我不需要监控烤箱对象的温度吗?

当我开始认为我是得到OOP!

谢谢,

Ron

>>I was reading a tutorial on OOP principals and design and I stumbled on
something that I don''t understand. The tutorial used a Oven class. In
that class there was a private field called temperature.

class Oven {
private int _temperature;

public int Temperature {
get {
return _temperature;
}
set {
_temperature = value;
}
}
}

This is the implementation I would normally employ. BUT... The article
says...
"The type Oven exposes its temperature as a property, which is a direct
reference to the variable _temperature. This means that the internal oven
temperature implementation is tied directly to its external interface.
The point of good object-oriented programming is to avoid this sort of
programming."

That seems strange to me...what if I have to expose temperature? What if
my cookie object needs to preheat the oven to 350 degrees dont I need to
monitor the temperature of the oven object?

Damn just when I started to think I was getting OOP!

Thanks,

Ron



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

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