我问这个问题纯粹是为了问题的速度方面.
从私有或公共(Java)获取对象的值之间的速度差异是什么?
class MyClass {
public int myInt = 5;
}
class MyOtherClass {
private int myInt = 5;
public int getMyInt() {
return myInt;
}
}
class MyMainClass {
public static void main (String [] args) {
MyClass myObject = new MyClass();
MyOtherClass myOtherObject = new MyOtherClass();
// which is faster?
System.out.println(myObject.myInt);
System.out.println(myOtherObject.getMyInt ());
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以测试它,但如果有人知道它,它不会伤害:)提前谢谢!