简体   繁体   English

Eclipse-为什么生成Getter和Setters不考虑“按参考”

[英]Eclipse - How come Generate Getters and Setters does not consider “By Reference”

if java is always pass variables by reference, why does eclipse generate the bean with out any consideration. 如果java总是通过引用传递变量,那么eclipse为什么不考虑任何因素就生成bean。

instead of: return myStr; 代替: return myStr;

needs to be return new String(myStr); 需要return new String(myStr);

no? 没有?

Edit 编辑
Ok, my example was bad. 好的,我的例子很糟糕。
lets leave eclipse, When I want to return a Custom object. 让离开日食,当我想返回一个自定义对象。 Do i need to create a "copy constructor" and return it, like that: 我是否需要创建一个“复制构造函数”并返回它,就像这样:

return new MyCustomObject(myCustomObject);


class MyCustomObject{

  private String str; 
  public MyCustomObject(String str){
    this.str = str;
  }

  public MyCustomObject(MyCustomObject obj){
    this.str =  obj.str;    
  }
}

Must I write that? 我必须写吗?

No. 没有。

In Java, every object variable is a reference. 在Java中,每个对象变量都是一个引用。 Objects cannot be passed by value, only primitives can (and always are). 对象不能按值传递,只有基元可以(并且总是如此)。 Well, that's slightly misleading. 好吧,这有点误导。 The reference is passed by value, but you can think of everything being a reference, just not in a C++ sense. 引用是通过值传递的,但是您可以将所有内容视为引用,只是不是C ++的意思。

Perhaps it's easiest to use an example. 也许使用示例最简单。

SomeObject foo;

public void doSomething(SomeObject bar) {
    bar.whatever();
    bar = new SomeObject();
    bar.someOtherMethod();
}

public void doStuff() {
    foo = new SomeObject();
    doSomething(foo);
}

So, foo is a reference to an instance of SomeObject . 因此, foo是对SomeObject实例的SomeObject When doSomething is called, the value of that reference is copied to bar , so now foo and bar are references to the same SomeObject . 调用doSomething ,该引用的值将复制到bar ,因此现在foobar是对同一SomeObject引用。

The line bar.whatever() calls whatever on the same object that foo refers to. bar.whatever() foo引用的同一对象上调用whatever

bar = new SomeObject() means that foo and bar now refer to different SomeObject instances, so someOtherMethod is not called on the object that foo refers to. bar = new SomeObject()意味着foobar现在引用了不同的 SomeObject实例,因此在foo引用的对象上调用someOtherMethod

This is completely different to C++, where 这与C ++完全不同

void doSomething(SomeObject& bar) {
    bar = whatever;
}

has a totally different meaning. 具有完全不同的含义。 You really should not ever think of Java in C++ terms. 您真的不应该用C ++术语来思考Java。

Regarding your example, String s are immutable in Java so it wouldn't matter even if objects could be passed by value. 对于您的示例, String在Java中是不可变的,因此即使可以通过值传递对象没有关系。

Regarding your second example, if you want to return an object that the caller cannot use to pollute your internal state then, yes, you need to have a copy constructor (or something equivalent). 关于第二个示例,如果要返回调用者无法用来污染您的内部状态的对象,那么是的,您需要具有复制构造函数(或类似的东西)。

For example: 例如:

class ClassWithInternalList {
    private List<String> reallyImportantData;

    public List<String> getImmutableViewOfData() {
        // Take advantage of the java.util.Collections tool for making a List immutable.
        return Collections.unmodifiableList(reallyImportantData);
    }

    public List<String> getSafeCopyOfData() {
        // Return a copy that the caller can modify without changing reallyImportantData.
        return new ArrayList<String>(reallyImportantData);
    }

    public List<String> justGetTheData() {
        // Return a reference to reallyImportantData that the caller can modify at will.
        return reallyImportantData;
    }
}

You can choose the appropriate type of return value (normal reference, immutable view or copy) depending on the situation. 您可以根据情况选择适当的返回值类型(正常参考,不变视图或副本)。 Any or all of the three options could be appropriate depending on exactly what you are doing. 这三个选项中的任何一个或全部都可能适合,具体取决于您正在做什么。

java.util.Collections makes it easy to get an immutable view of a Collection , but for custom classes you'll need to do your own immutable-ness. java.util.Collections使得获取Collection的不变视图变得容易,但是对于自定义类,您需要做自己的不变性。

Remember that you only need to do this if there is an issue with mutability. 请记住,只有在可变性存在问题时才需要这样做。 Your MyCustomObject example is still immutable (since the caller cannot change any state in the returned MyCustomObject instance), so the question is still kinda moot. 您的MyCustomObject示例仍然是不可变的(因为调用者无法更改返回的MyCustomObject实例中的任何状态),因此问题仍然是有争议的。

If the variable is primitive or immutable,you modify what returned won't influence the original property. 如果变量是原始变量或不可变变量,则您修改返回的内容将不会影响原始属性。

Other condition, the variable is a custom type,maybe your class defines a constructor with itself like String,maybe not . 其他情况下,变量是自定义类型,也许您的类像String一样定义了自己的构造函数,也许不是。 So eclipse does not know how to generate it's copy. 因此,eclipse不知道如何生成它的副本。

As all knows,java pass reference,so java programmers are used to return the reference and allow other modify.Eclipse implement it as default. 众所周知,java通过了引用,因此java程序员习惯于返回引用并允许其他修改。Eclipse将其默认实现。

If you don't like it ,you can return a copy or implements Immutable. 如果您不喜欢它,则可以返回副本或实现Immutable。

You usually do not want to create a new object when using getter. 使用getter时,通常不希望创建新对象。 What you usually want is to get the reference to object's attribute to work with it easier, for encapsulation and to have the right value even when something else changes the value of the attribute before you use it. 通常,您希望获得对对象属性的引用,以使其更容易使用,进行封装并具有正确的值,即使在使用该属性之前其他更改属性值的情况下也是如此。

Creating a new instance of an object by method which is called often, is not expected to do it and even does not declare doing such thing is freaking anti-pattern . 用通常被调用的方法创建对象的新实例是不希望这样做的,甚至没有声明这样做是在反模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM