为什么 Java 没有像 C# 这样的自动属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3429139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:48:30  来源:igfitidea点击:

Why doesn't Java have automatic properties like C#?

c#javahistorylanguage-designautomatic-properties

提问by Sergio Tapia

C# has automatic properties which greatly simplify your code:

C# 具有自动属性,可大大简化您的代码:

public string Name { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }

Whereas Java has you write this much code:

而 Java 却让你写了这么多代码:

private String name;
private String middleName;
private String LastName;

public String Name(){
   return this.name;
}

etc..

Is there a particular reason Java hasn't implemented something like this?

Java 没有实现这样的东西有什么特别的原因吗?

采纳答案by Jesper

It is not so easy to add new features to an existing programming language, especially if you care about backward compatibility. Sun has always been extremely careful with adding new features to Java, because they wanted to be absolutely sure that any new language feature will not break the millions of Java programs that have been written over the years.

向现有编程语言添加新功能并不容易,特别是如果您关心向后兼容性。Sun 在向 Java 添加新功能时一直非常小心,因为他们希望绝对确保任何新的语言功能都不会破坏多年来编写的数百万个 Java 程序。

So, it's not just a question of adding this to the language; you have to think very carefully, and try it out, to discover if there aren't any subtle backward compatibility problems with whatever new feature you want to add.

因此,这不仅仅是将其添加到语言中的问题;您必须非常仔细地考虑并尝试一下,以发现您要添加的任何新功能是否存在任何微妙的向后兼容性问题。

There have been proposals to add support for properties in one form or another in Java, but it looks like for Java 7 (the next version coming up) this is not a feature that is being considered.

已经有人提议在 Java 中以一种或另一种形式添加对属性的支持,但看起来对于 Java 7(即将推出的下一个版本)这不是正在考虑的功能。

You might want to have a look at Project Lombok, which is a kind of extension to Java, using annotations, to make it possible to write more concise code (it can, for example, automatically generate getters and setters for fields).

您可能想看看Project Lombok,它是 Java 的一种扩展,它使用注释来编写更简洁的代码(例如,它可以自动为字段生成 getter 和 setter)。

回答by Noon Silk

Yep, because it doesn't have it. As the saying goes, all features start out unimplemented.

是的,因为它没有。俗话说,所有功能一开始都没有实现。

回答by Jon Hanna

Automatic properties are built upon properties.

自动属性建立在属性之上。

Properties in C# are a language feature, in Java they are a convention (methods starting with get or set are often considered properties by people talking about the code, but to the compiler it's no different than foo or bar).

C# 中的属性是一种语言特性,在 Java 中它们是一种约定(以 get 或 set 开头的方法通常被谈论代码的人认为是属性,但对编译器来说,它与 foo 或 bar 没有什么不同)。

.NET and its associated languages where in many ways based upon COM (sometimes in following suit, sometimes in deliberately notdoing something in COM that was for some reason or other unpopular).

.NET 及其相关语言在很多方面都基于 COM(有时效仿,有时故意不在COM 中做一些出于某种原因或其他不受欢迎的事情)。

COM has a concept of properties which in VB was backed by language features (in C++ it was backed by conventions).

COM 有一个属性概念,在 VB 中由语言特性支持(在 C++ 中它由约定支持)。

Early versions of VB, especially in contexts where it was used to provide basic programmatic access to object models provided from elsewhere, aimed to simplify the object models presented to users to make the distinction between a field and a method that gets or sets (perhaps with additional work, perhaps not) unimportant (consider that while they differ in some important way from the outside in .NET, syntactically accessing a property and a public field is the same). When VB and COM (and before that OLE) grew up, they grew up together.

VB 的早期版本,特别是在它用于提供对从其他地方提供的对象模型的基本编程访问的上下文中,旨在简化呈现给用户的对象模型,以区分字段和获取或设置的方法(也许与额外的工作,也许不是)不重要的(考虑到它们在某些重要方面与 .NET 中的外部不同,但在语法上访问属性和公共字段是相同的)。当 VB 和 COM(以及在那个 OLE 出现之前)长大时,它们一起长大。

So in all, while C# shares a C/C++ inheritance with Java, it also has an inheritance that Java does not share, which made properties seem like a good idea to C#'s creators, but not to Java's.

因此,总而言之,虽然 C# 与 Java 共享 C/C++ 继承,但它也具有 Java 不共享的继承,这使得属性对于 C# 的创建者来说似乎是一个好主意,但对 Java 的创建者来说却不是。

Edit: At first I said:

编辑:起初我说:



Personally, I think automatic properties are really a workaround to a flaw in properties, rather than a way to simplify your code. Properties "look" like public fields syntactically, but aren't entirely (try using DataBinder.Evalto retrieve a field). If a property was both fully public, and had no additional functionality in getter or setter (which is the case with automatic properties), I'd rather just have a public field (encapsulation is no argument here, as fully exposing it publicly by-passes that anyway), but the differences between fields and properties works against that.

就我个人而言,我认为自动属性实际上是解决属性缺陷的一种方法,而不是一种简化代码的方法。属性在语法上“看起来”像公共字段,但不完全是(尝试使用DataBinder.Eval检索字段)。如果一个属性是完全公开的,并且在 getter 或 setter 中没有额外的功能(自动属性就是这种情况),我宁愿只有一个公共字段(这里没有封装,因为完全公开它 -无论如何都会通过),但字段和属性之间的差异与此相反。



I retract that statement. Making fields exactly like properties would require the fields of simple Plain-Old-Data structs to act like properties, which would be a performance loose. Conceptually, I'd like them to be more like each other, but whenever I think on it (and I've gone from being annoyed to going "oh wait" like here more than once), it's better as it is.

我收回那句话。使字段与属性完全相同将需要简单的普通旧数据结构的字段来充当属性,这会降低性能。从概念上讲,我希望它们更像彼此,但是每当我想到它时(而且我已经不止一次从恼火变成像这里一样“哦等等”),它仍然更好。

回答by Ramesh Soni

For same reason the C# 2.0 or lower does't have this. It's more or syntactical sugar rather a language feature. There are ton of features like this which could be added to any language, but not one knows why they are not the language.

出于同样的原因,C# 2.0 或更低版本没有这个。它更多是语法糖而不是语言功能。有很多这样的功能可以添加到任何语言中,但没有人知道为什么它们不是语言。

回答by cHao

.net came along after Java, and one of its goals was to interoperate with COM. COM had properties, probably a la VB, so .net all but had to add them in order to make languages interoperable. (That, and they were a pretty spiffy idea.)

.net 在 Java 之后出现,其目标之一是与 COM 互操作。COM 具有属性,可能类似于 VB,因此 .net 几乎必须添加它们以使语言具有互操作性。(那个,他们是一个非常漂亮的主意。)

Java didn't have such a need, and its creators probably didn't want to pollute the meaning of "=" or make function calls look like member variables. (They are -- or at least were, at some point -- big on keeping the language as clean as possible.) Their approach was instead the Java bean specs, which specified a naming convention for getters and setters. IDEs that know the spec can more-or-less fudge the view of the getter and setter as a single "property" for design purposes.

Java 没有这样的需求,它的创造者可能不想污染“=”的含义,或者让函数调用看起来像成员变量。(他们 - 或者至少在某些时候 - 在保持语言尽可能干净方面很重要。)他们的方法是 Java bean 规范,它指定了 getter 和 setter 的命名约定。了解规范的 IDE 可以或多或少地将 getter 和 setter 的视图作为设计目的的单个“属性”。