Tag: getter setter

Java Setter和Getter

始终建议使用getter / setter访问私有变量。 为什么将它们声明为公共并访问它们不是更好的主意。 无论如何,我们使用getter和setter访问它?

IntelliJ getter / setter格式

如何让IntelliJ在一行上生成getter / setter,如下所示: public String getAbc() { return abc; } 代替 public String getAbc() { return abc; } ??

数组的getter和setter

我有几个关于数组的getter和setter的问题。 假设我们有一个这样的类,它在构造函数中创建一个数组的私有副本: import java.util.Arrays; public class Foo { private int[] array; public Foo(int[] array) { this.array = Arrays.copyOf(array, array.length); } } 我们希望只通过getter和setter访问/变异数组。 如果我们有一个看起来像这样的吸气剂: public int[] getArray() { return array; } 当我们返回一个允许用户直接修改数组元素的引用时,它会破坏getter的目的。 例如 Foo foo = new Foo(someArray); … int[] bar = foo.getArray(); bar[0] = 123; // Now foo.array[0] = 123 and we haven’t used a […]

与Java Encapsulation Concept混淆

美好的一天! 我正在阅读一本关于封装的Java书,它提到了getter和setter方法。 我已经读过要隐藏属性,我必须将我的实例变量标记为“PRIVATE” ,并使用getter and setter的“PUBLIC”方法来访问数据。 所以我尝试制作类似但不是传统的代码,如下所示: public class AddressBookEntry { private String name; private String address; private String telNo; private String email; public void getAllInfo() { name = JOptionPane.showInputDialog(“Enter Name: “); address = JOptionPane.showInputDialog(“Enter Address: “); telNo = JOptionPane.showInputDialog(“Enter Tel. No: “); email = JOptionPane.showInputDialog(“Enter Email Address: “); } } 上面的代码是否暴露了我的变量,因为我直接分配了它? 我怎么能做得更好? 如果我改为使用传统的getter and […]

在java中使用getter方法向List添加元素是不好的做法?

假设我在类中有一个private ArrayList或LinkedList ,我永远不会为它分配新的引用,换句话说,这将永远不会发生: myLinkedList = anotherLinkedList; 这样我就不需要使用setMyLinkedList(anotherLinkedList) 。 但! 我需要添加元素,或从中删除元素。 我应该只编写一种新的setter ,执行adding而不是setting的任务,比如myLinkedList.add(someElement) ? 或者可以通过使用getter来执行此操作,而不必违反Encapsulation主体? getMyLinkedList().add(someElement) (+假设如果我不服从封装,我将失去我的标记: – “)

Java:Getter和setter比直接访问更快?

我测试了我在Linux上网本上使用VisualVM 1.3.7编写的Java光线跟踪器的性能。 我用剖析器测量过。 为了好玩,我测试了使用getter和setter以及直接访问字段之间的区别。 getter和setter是标准代码,没有添加。 我没想到会有任何分歧。 但是直接访问代码的速度较慢。 这是我在Vector3D中测试的样本: public float dot(Vector3D other) { return x * other.x + y * other.y + z * other.z; } 时间:1542毫秒/ 1,000,000次调用 public float dot(Vector3D other) { return getX() * other.getX() + getY() * other.getY() + getZ() * other.getZ(); } 时间:1453毫秒/ 1,000,000次调用 我没有在微基准测试中测试它,而是在光线跟踪器中测试它。 我测试代码的方式: 我用第一个代码启动了程序并进行了设置。 光线跟踪器尚未运行。 我启动了分析器并在初始化完成后等了一会儿。 我开始了一个光线追踪器。 当VisualVM显示足够的调用时,我停止了探查器并等了一会儿。 […]

Getter-Setter和私有变量

如果我可以通过getter-return引用更改私有变量的值,那么它是否绕过setter方法? 它不会破坏getter-setter和私有变量的目的 public class Test{ private Dimension cannotBeChanged; public Test(int height, int width) { if(height!=3) cannotBeChanged.height = height; if(width!=3) cannotBeChanged.width = width; } public Dimension getDimension() { return cannotBeChanged; } public void setDimension(int height, int width) { if(height!=3) cannotBeChanged.height = height; if(width!=3) cannotBeChanged.width = width; } public static void main(String [] args) { Test testOne […]

最合适的边界检查 – 构造函数或setter?

对Java来说还是比较新的,我想知道哪个是处理这个问题的更好方法。 我有一个类构造函数,它接受一些参数,在这个类中也是公共getter和setter: private String name; private Float value; public MySampleClass(String theName, Float theValue) { setName(theName); setValue(theValue); } public void setName(String n) { this.name = n; } public value setValue(Float v) { this.value = v; } 我想对这个Float做一些检查。 看起来最好把它放在setter中: public value setValue(Float v) { if (v 1.0f) { this.value = 1.0f; } } 这段代码最初在构造函数中检查了边界,并在setter中再次检查,这似乎是多余的。 我更改了构造函数以调用setter并将检查放在那里。 那更有意义吗? 或者我违反了一些我完全不知道的惯例?

为什么要声明getter和setter方法是私有的?

我看到了一个代码,其中getter和setter方法被声明为private。 我试图找出它背后的逻辑,我真的很难理解为什么你会将它们声明为私有? 这与我们试图通过getter和setter实现的完全相反。

Eclipse JDT:是否有重构用setter / getter方法替换直接字段访问?

我知道我可以在Eclipse源代码菜单中为字段生成setter和getter,但我很惊讶它没有提供用新调用方法调用来替换直接字段访问器。 有谁知道如何做这个缺乏手动搜索和替换? 谢谢!