新建了个OneCavans类构造函数如下
public OneCanvas() {
width = this.getWidth();
height = this.getHeight();
System.out.println( "width: " + width);
System.out.println( "height " + height);
System.out.println( "hasPointerEvents: " + this.hasPointerEvents());
System.out.println( "hasPointerMotionEvents: "
+ this.hasPointerMotionEvents());
System.out.println( "hasRepeatEvents: " + this.hasRepeatEvents());
System.out.println( "isDoubleBuffered: " + this.isDoubleBuffered());
cmd = new Command[8];
cmd[0] = new Command( "Line ", Command.OK, 1);
cmd[1] = new Command( "Arc ", Command.OK, 1);
cmd[2] = new Command( "FillArc ", Command.OK, 1);
cmd[3] = new Command( "Rect ", Command.OK, 1);
cmd[4] = new Command( "RoundRect ", Command.OK, 1);
for (int i = 0; i < 5; i++) {
this.addCommand(cmd[i]);
}
this.setCommandListener(this);
请问最后一行里的最后一个this是指的什么呢,再有在测试类中:
public class OneCanvasMid extends MIDlet {
OneCanvas ocs;
static OneCanvasMid inc;
public OneCanvasMid() {
inc = this;
ocs = new OneCanvas();
}
protected void startApp() {
System.out.println( "============startApp======== ");
Display.getDisplay(this).setCurrent(ocs);
System.out.println( "############startApp######## ");
}
inc=this,this是指前面的构造函数么?Display.getDisplay(this).setCurrent(ocs);又是什么意思???
问题问多了点,请高手指点,高分送上
------解决方案--------------------------------------------------------
this没有什么特别的意思,就是当前对象的指针,它不会指构造函数。
第一问里最后一行的得最后一个this和前面的那个this意思是一样的,都是指正在构造的OneCanvas的对象,这行代码调用OneCanvas类的实例方法,并把自己当作参数传进去。
第二问:OneCanvasMid 类中定义了一个静态成员OneCanvasMid inc,inc = this自然就是将当前构造的OneCanvasMid 对象赋值给inc这个静态成员。(这样的效果就是inc总是最后一个被构造的OneCanvasMid 对象)
第三问和第一问基本是一个意思,调用Display类的貌似是静态方法getDisplay();并且把自己(OneCanvasMid 的实例)作为参数传进去。