关于财产的OOP问题&方法 [英] OOP question about properties & methods

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

问题描述

让我们考虑一个名为Currency的类。这个班级必须负责

负责货币兑换的所有计算,这样我就可以用欧元传递价值,并以美元的形式返回转换后的价值对于

例子......

好​​吧,我可以用两种方式做到这一点,如下所示:


1)我创建这样的方法:


double ConvertValue(double OriginalValue){

//这里我计算值

返回ComputedValue;

}

2)我创建这样的方法:


bool ConvertValue(double OriginalValue){

//这里我加载私有属性ConvertedValue


ConvertedValue = ComputedValue


返回OK;

}

在第一种情况下,我在方法本身返回值,在第二种情况下,我将
加载到私有属性中。在第二种情况下,客户端方法

必须执行ConvertValue然后询问属性值。在第一个

的情况下,返回的值将在方法返回自身....


什么是最好的方法?是否有特殊情况我们应该使用一个或另一个?

解决方案

Bruce One< ra ** @ virtualsoftware.com.brwrote:


让我们考虑一个名为Currency的类。这个班级必须负责

负责货币兑换的所有计算,这样我就可以用欧元传递价值,并以美元的形式返回转换后的价值对于

示例...

好​​吧,我可以通过两种方式执行此操作,如下所示:



< snip>


在第一种情况下,我在方法本身返回了值,在第二种情况下我将

加载到私有中属性。在第二种情况下,客户端方法

必须执行ConvertValue然后询问属性值。在第一个

的情况下,返回的值将在方法返回自身....


什么是最好的方法?是否有特殊情况我们应该使用一个或另一个?b $ b?



第一种方法肯定更好。这意味着:


1)来电者只需做一件事就可以做他们想要的事情 -

获得转换后的价值。


2)如果您使用例外来表示意外故障,则呼叫者

不需要做任何事情以使安全一旦发生错误,

中止的行为


3)如果操作*确实*失败(并抛出异常)

异常可以提供比简单的真/假更丰富的细节


-

Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件


那么在哪种情况下我会采用第二种方法?


" Jon Skeet [C#MVP]"写道:


Bruce One< ra ** @ virtualsoftware.com.brwrote:


让我们考虑一下一个叫做货币的类。这个班级必须负责

负责货币兑换的所有计算,这样我就可以用欧元传递价值,并以美元的形式返回转换后的价值对于

例子......

好​​吧,我可以用两种方式做到这一点,如下所示:



< snip>


在第一种情况下,我在方法本身返回了值,在第二种情况下我将

加载到私有中属性。在第二种情况下,客户端方法

必须执行ConvertValue然后询问属性值。在第一个

的情况下,返回的值将在方法返回自身....


什么是最好的方法?是否有特殊情况我们应该使用一个或另一个?b $ b?



第一种方法肯定更好。这意味着:


1)来电者只需做一件事就可以做他们想要的事情 -

获得转换后的价值。


2)如果您使用例外来表示意外故障,则呼叫者

不需要做任何事情以使安全一旦发生错误,

中止的行为


3)如果操作*确实*失败(并抛出异常)

异常可以提供比简单的真/假更丰富的细节


-

Jon Skeet - < sk *** @ pobox .com>
http://www.pobox.com/~skeet博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件


Bruce One< ra ** @ virtualsoftware。 com.brwrote:


那么在哪种情况下我会使用第二种方法?



这里答案真的没用 - 换句话说,

很少。或者,答案是指答案。可能不是一个有意义的

概念,如果对象状态的许多部分发生变化。


如果你有希望表明成功的情况/失败

没有使用异常,顺便说一句,你应该使用与Double.TryParse相同的

模式,带有输出参数。

-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件


Lets consider a class called Currency. This class must be the responsible for
taking care of all calculations over currency exchanges, in such a way that I
pass values in Euros and it returns the converted value in Dollar for
example...
Well, I might do this in 2 ways, as below:

1) I create a method like this:

double ConvertValue (double OriginalValue) {
// here I compute the value
return ComputedValue;
}
2) I create the method like this:

bool ConvertValue (double OriginalValue) {
// here I load private attribute ConvertedValue

ConvertedValue = ComputedValue

return OK;
}
In the first case I returned value in the method itself, in second case I
loaded the value in a private attribute. In second case the client method
would have to execute ConvertValue and then ask for property value. In first
case, the returned value would come in the method return itself....

What is the best approach? Are there special situations in which we should
use one or another??

解决方案

Bruce One <ra**@virtualsoftware.com.brwrote:

Lets consider a class called Currency. This class must be the responsible for
taking care of all calculations over currency exchanges, in such a way that I
pass values in Euros and it returns the converted value in Dollar for
example...
Well, I might do this in 2 ways, as below:

<snip>

In the first case I returned value in the method itself, in second case I
loaded the value in a private attribute. In second case the client method
would have to execute ConvertValue and then ask for property value. In first
case, the returned value would come in the method return itself....

What is the best approach? Are there special situations in which we should
use one or another??

The first approach is definitely better. It means:

1) The caller only has to do one thing in order to do what they want -
get the converted value.

2) If you use exceptions to indicate unexpected failures, the caller
doesn''t need to do anything in order to have the "safe" behaviour of
aborting as soon as an error occurs

3) If the operation *does* fail (and an exception is thrown) the
exception can give far richer detail than a simple true/false could

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


So in which case would I use second approach?

"Jon Skeet [C# MVP]" wrote:

Bruce One <ra**@virtualsoftware.com.brwrote:

Lets consider a class called Currency. This class must be the responsible for
taking care of all calculations over currency exchanges, in such a way that I
pass values in Euros and it returns the converted value in Dollar for
example...
Well, I might do this in 2 ways, as below:


<snip>

In the first case I returned value in the method itself, in second case I
loaded the value in a private attribute. In second case the client method
would have to execute ConvertValue and then ask for property value. In first
case, the returned value would come in the method return itself....

What is the best approach? Are there special situations in which we should
use one or another??


The first approach is definitely better. It means:

1) The caller only has to do one thing in order to do what they want -
get the converted value.

2) If you use exceptions to indicate unexpected failures, the caller
doesn''t need to do anything in order to have the "safe" behaviour of
aborting as soon as an error occurs

3) If the operation *does* fail (and an exception is thrown) the
exception can give far richer detail than a simple true/false could

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Bruce One <ra**@virtualsoftware.com.brwrote:

So in which case would I use second approach?

Where the answer genuinely isn''t useful at this point - in other words,
pretty rarely. Alternatively, "the answer" may not be a meaningful
concept, if many parts of the state of the object change.

If you have a situation where you wish to indicate success/failure
without using an exception, by the way, you should usually use the same
pattern as Double.TryParse, with an output parameter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


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

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