书上说:
you can also use the this reference as an argument to a method, in which an object passes a reference of itself to another object.翻译过来大概是:
你也可以把this引用作为方法的实参,把一个对象自己的引用传给另一个对象。请问这是怎么一种情况呢?这句抽象的话该怎样理解呢?哪位大侠可以给个实例么?
you can also use the this reference as an argument to a method, in which an object passes a reference of itself to another object.翻译过来大概是:
你也可以把this引用作为方法的实参,把一个对象自己的引用传给另一个对象。请问这是怎么一种情况呢?这句抽象的话该怎样理解呢?哪位大侠可以给个实例么?
解决方案 »
- serial中的getInputString与getOutputString的具体用法。
- 请教高手关于泛型的问题
- 两道关于THEARD的问题,谢谢了!
- 正则表达式 如何在查找的内容中过滤
- c++和java的访问权限问题,糊涂了
- 《面向对象JAVA编程思想》这本书怎么样?
- 一个很菜的问题!
- JTextArea的问题,关于线程(100+100)
- 小妹求助,javahelp怎样用在APPLET中还有就是用在WEB START,在线等待谢谢了。QQ25096679
- 新人急求!!!java怎么操作一个已有的Sqlite文件是以.sqb结尾的文件
- javamail发送邮件问题
- 2个对象各自有一个指向对方的引用,他们可以被垃圾回收么?
Classroom Q & A
Q: The this reference seems a little confusing. Is it necessary?
A: The this reference is one of those fundamental concepts of OOP
that tends to be confusing the first time you see it. To explain the
need for the this reference, I always try to emphasize the fact that
a field or method cannot be accessed without a reference. The this
reference is the only way a method in a class can access the other
fields or methods of the class.
Q: Why does the compiler add this. automatically?
A: It is strictly for convenience. It would be fairly tedious (although
certainly feasible) to add this. every time it was required in a class.
In a large class, there could easily be hundreds of accesses to the
fields or methods of the class, each requiring the this reference.
For now, keep in mind that the this reference is implicitly being
used when a method in your class accesses a field of the class.
{
public static void main(String args[])
{
A a = new A();
B b = new B(a);
a.fun();
}
}class A
{
B b;
public A()
{
} public void setB(B b)
{
this.b = b;
} public void fun()
{
System.out.println(b.i);
}
}class B
{
int i=3;
public B(A a)
{
a.setB(this);
}
}
public class A { public static void main(String[] args) { new B().method(this); }}class B {
public void method(A a) { }
}
public void method(A a) { }
}
当前对象的引用啊,引用当作参数