Java DOT运算符的目的是什么? [英] What is the purpose of Java DOT operator?
问题描述
有人可以告诉我Java DOT运算符实际上是做什么的吗?
Can somebody tell me what Java DOT operator actually does?
例如:
public class {
int value;
public void great() {};
...
}
public static void main(String[] args) {
Person p = new Person();
Person.great(); // <--- here
Person.value; // <--- here
我想知道在执行Person.great()
或Person.value
时上述代码中的 .
运算符是做什么的吗?
I want to know what is .
operator doing in above code when I do Person.great()
or Person.value
?
推荐答案
点运算符,也称为分隔符或句点,用于将变量或方法与参考变量分开.
The dot operator, also known as separator or period used to separate a variable or method from a reference variable.
使用类名只能访问静态变量或方法.
Only static variables or methods can be accessed using class name.
对象类之外的代码必须使用对象引用或表达式,然后使用点(.)运算符,然后使用简单的字段名称,如
Code that is outside the object's class must use an object reference or expression, followed by the dot (.) operator, followed by a simple field name as in
objectReference.fieldName
我们使用对象引用来调用对象的方法.将该方法的简单名称附加到对象引用中,并使用一个中间的点运算符(.),如
We use the object reference to invoke an object's method. Append the method's simple name to the object reference, with an intervening dot operator (.) as in
objectReference.methodName(argumentList);
在上述代码中,可以使用 p.great() 来调用方法 great() 对象 p 和 p.value 上的strong>用于访问实例变量 值 .
In the above mentioned code, p.great() can be used to invoke the method great() on object p and p.value is used to access the instance variable value.
参考: https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
完整参考书,由赫伯特·希尔德(Herbert Schildt)撰写
The Complete Reference, Book by Herbert Schildt
这篇关于Java DOT运算符的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!