Java中缺少属性语法 [英] Absence of property syntax in Java

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

问题描述

C#具有用于声明和使用属性的语法.例如,可以声明一个简单的属性,如下所示:

C# has syntax for declaring and using properties. For example, one can declare a simple property, like this:

public int Size { get; set; }

还可以在属性中添加一些逻辑,如下所示:

One can also put a bit of logic into the property, like this:

public string SizeHex
{
    get
    {
        return String.Format("{0:X}", Size);
    }
    set
    {
        Size = int.Parse(value, NumberStyles.HexNumber);
    }
}

无论是否具有逻辑,属性的使用方式与字段相同:

Regardless of whether it has logic or not, a property is used in the same way as a field:

int fileSize = myFile.Size;

我对Java或C#都不陌生-我已经使用了很多,而且我总是想念在Java中使用属性语法.我在这个问题中读到,添加财产支持的可能性很小Java 7或以前的版本",但坦率地说,我发现要在讨论,论坛,博客,评论和JSR中挖掘太多的工作,以找出原因.

I'm no stranger to either Java or C# -- I've used both quite a lot and I've always missed having property syntax in Java. I've read in this question that "it's highly unlikely that property support will be added in Java 7 or perhaps ever", but frankly I find it too much work to dig around in discussions, forums, blogs, comments and JSRs to find out why.

所以我的问题是:有人能总结一下为什么Java不太可能获得属性语法吗?

So my question is: can anyone sum up why Java isn't likely to get property syntax?

  • 是因为与其他可能的改进相比,它还不够重要吗?
  • 是否存在技术限制(例如与JVM相关的限制)?
  • 这是政治问题吗?(例如我使用Java进行编码已有50年了,我说我们不需要steenkin'属性!" )
  • 是否属于骑自行车的人?

推荐答案

我认为这只是Java对待事物的一般哲学.属性有些神奇",Java的哲学是保持核心语言尽可能简单,避免像瘟疫这样的魔术.这使Java成为几乎所有程序员都可以理解的<通用语言>.这也使得推理任意孤立的代码段的行为非常容易,并且可以提供更好的工具支持.不利的一面是它使语言更加冗长,表达能力更差.这不一定是设计语言的正确方法或错误方法,而只是权衡取舍.

I think it's just Java's general philosophy towards things. Properties are somewhat "magical", and Java's philosophy is to keep the core language as simple as possible and avoid magic like the plague. This enables Java to be a lingua franca that can be understood by just about any programmer. It also makes it very easy to reason about what an arbitrary isolated piece of code is doing, and enables better tool support. The downside is that it makes the language more verbose and less expressive. This is not necessarily the right way or the wrong way to design a language, it's just a tradeoff.

这篇关于Java中缺少属性语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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