Java DOT 运算符的目的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37646364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of Java DOT operator?
提问by user2443943
Can somebody tell me what Java DOT operator actually does?
有人能告诉我 Java DOT 运算符实际上是做什么的吗?
For example:
例如:
public class {
int value;
public void great() {};
...
}
public static void main(String[] args) {
Person p = new Person();
Person.great(); // <--- here
Person.value; // <--- here
I want to know what is .
operator doing in above code when I do Person.great()
or Person.value
?
我想知道什么是.
运营商在上面的代码做当我Person.great()
还是Person.value
?
回答by Karthik Reddy V V
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);
In the above mentioned code, p.great()can be used to invoke the method great()on object pand p.valueis used to access the instance variable value.
在上述代码中,p.great()可被用于调用该方法很大的()上对象p和p.value用于访问实例变量值。
Ref: https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
参考:https: //docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
The Complete Reference, Book by Herbert Schildt
Herbert Schildt 所著的完整参考书
回答by Johannes H.
.
is not an operator. Therefore, it "does" nothing.
.
不是运营商。因此,它“什么都不做”。
It is just a syntactic element that denotes the seperation of, in this case, a variable name holding an object and the object's property. The same character is used to seperate package names and Classes.
它只是一个句法元素,在这种情况下,它表示保存对象的变量名和对象属性的分离。相同的字符用于分隔包名称和类。