Why will there be no native properties in Java 7?

后端 未结 5 1781
陌清茗
陌清茗 2021-02-18 21:57

Is there any rational reason, why native properties will not be part of Java 7?

5条回答
  •  后悔当初
    2021-02-18 22:34

    There are two more reasons to avoid properties in any language:

    • Properties are not very object-oriented. Making them easy to write encourages the pattern where the object just serves up its internal state and the caller manipulates it. The object should provide higher-level methods and keep its internals private. Next time you're tediously implementing a getter, consider what the caller will do with the data and whether you can just provide that functionality directly.

    • Properties encourage mutable state (through setters), which makes a program less parallelizable. As the number of cores goes up, we should all be trying to make our objects immutable to make concurrent reasoning easier. Next time you're tediously implementing a setter, consider removing it and making the object immutable.

提交回复
热议问题