你说的这个我知道,但是我就是找不到这种用法的具体介绍。[/quote]
User类里不是引入了一个Acount的变量acount么,该变量被认为是Acount的实例当然能调用自己的方法咯,但是实际用时注意acount需要被传值,不然会报错。[/quote]
为什么这个变量acount就可以被认为是Acount的实例?实例对象不是要new出来的吗?[/quote]
举个例子来说
A a=new A();
A b=a;
所以不一定要new,b被赋值a后也是A的实例。
对象引用调用实例方法,如果要找具体介绍,最权威的肯定就是官方给的.这是官方的Java SE 8版本的java语言规范
下面是4.3.1章节对Object的介绍,其中第二条就是方法调用,具体在15.12章节
The operators on references to objects are:
Field access, using either a qualified name (§6.6) or a field access expression (§15.11)
Method invocation (§15.12)
The cast operator (§5.5, §15.16)
The string concatenation operator + (§15.18.1), which, when given a String operand and a reference, will convert the reference to a String by invoking the toString method of the referenced object (using "null" if either the reference or the result of toString is a null reference), and then will produce a newly created String that is the concatenation of the two strings
The instanceof operator (§15.20.2)
The reference equality operators == and != (§15.21.3)
The conditional operator ? : (§15.25).
下面是15.12章节部分内容,这里是链接A method invocation expression is used to invoke a class or instance method.
MethodInvocation:
MethodName ( [ArgumentList] )
TypeName . [TypeArguments] Identifier ( [ArgumentList] )
ExpressionName . [TypeArguments] Identifier ( [ArgumentList] )
Primary . [TypeArguments] Identifier ( [ArgumentList] )
super . [TypeArguments] Identifier ( [ArgumentList] )
TypeName . super . [TypeArguments] Identifier ( [ArgumentList] )
ArgumentList:
Expression {, Expression}