奇怪的Java编码样式-为什么? [英] Strange Java Coding Style - Why?

查看:65
本文介绍了奇怪的Java编码样式-为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了一些有些奇怪的代码,我想知道是否有人可以阐明为什么这样写。

I've just came across some code which is slightly strange, I was wondering if anyone could shed light on why it might be written like this.

认为与并发有关-因此,如果另一个线程访问变量,则变量不会被更改(因为变量更新不是原子的)。 这是速度(因为局部变量比类级变量快?) OR 我在这里写的所有内容都错了:)

I think it's got something to do with concurrency - so that the variables can't get changed if another thread accesses it (because variable updates aren't atomic). Or it's speed (because local variables are faster than class level variables?) OR I'm wrong on everything I've written here :)

哦,我不是在说匈牙利符号,而是在讲方法中的本地赋值。

Oh, I'm not talking about the Hungarian notation, I'm talking about the local assignments inside the methods.

public Class Space
{
  private double m_dWidth = 0;

  // Constructors & other methods omitted for readability
  //...

  public double getWidth()
  {
     double dWidth = m_dWidth;
     return dWidth;
  }
}


推荐答案

它看起来写代码的人误解了返回的工作方式。在我的java学生(大学一年级)中,常见的误解是 return 仅适用于局部定义的变量,而不是任意表达式。

It looks like the person writing the code has misunderstood how return works. It is a common misunderstanding among my java students (university first year) that return only works on locally defined variables instead of arbitrary expressions.

正如@Steve所建议的,它也可能是较旧的,更复杂的代码版本的宿醉。哪一个更可能取决于您在哪里找到了代码。

As @Steve suggests, it could also be a hangover from an older, more complicated version of the code. Which one is more likely depends on where you found the code.

这篇关于奇怪的Java编码样式-为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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