Having following code:
class X
{
public void Y(){}
}
X _x=new X();
_x.Y(); //should I say Y is method of _x variable? It is easy but actually the variable contains just reference to object that has this method
X newX=x; //here I assign the value of variable x to variable newX. The value 开发者_StackOverflowis reference
In my most pedantic mood, I would write:
Y
is a parameterless instance method declared in typeX
. It is invoked on the object referred to by the value of_x
.
(_x
itself is neither the object nor the reference - it's the variable.)
Usually it's not much of a problem because classes and variables have descriptive names, so it's pretty clear what's what.
An example using an actual class:
builder
is a variable containing a reference to an instance of the StringBuilder
class:
StringBuilder builder = new StringBuilder();
The Append
method is a method in the StringBuilder
class, not a method in the builder
variable. You are calling the method on the instance that builder
is referring to:
builder.Append("asdf");
Y
is a pubilc instance method on the class X
being invoked on the instance of the class X
referred to by _x
.
As an aside, an invocation like this is actually compiled as something like
call X::Y(_x)
This is because every instance-level method has an implicit first parameter that is a reference to the object that the method is being invoked on (this is how this
refers to the right object).
精彩评论