gpt4 book ai didi

java - 在同一类的其他方法中使用 get 方法的 Java 约定是什么?

转载 作者:搜寻专家 更新时间:2023-11-01 01:44:49 25 4
gpt4 key购买 nike

我在java类中写了get方法后,是在同一个类中使用get方法好还是变量本身好?

例如:

if(a.getWidth()>this.getWidth())

或:

if(a.getWidth()>this.width)

我也很困惑我是否应该如此频繁地使用 this.anything。将相同类型的对象相互比较时,它似乎更容易阅读。

最佳答案

is it better to use the get method in the same class or the variable itself?

恕我直言,使用变量。访问器方法主要供其他对象使用。

Also I am confused if i should be using the this.anything so much. It seemed easier to read when comparing objects of the same type to each other.

您并不总是需要显式使用this 引用。正如您所说,它主要用于提高可读性。

关于java - 在同一类的其他方法中使用 get 方法的 Java 约定是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14493892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com