请帮忙看下面程序中的几个this各指什么啊?import java.applet.*;import java.awt.*;
import java.awt.event.*;import javax.swing.*;
class MyPanel extends JPanel{
  int x;JLabel label;
  MyPanel(int a){
    x=a;getSize();
    label=new JLabel("我是第"+x+"个标签");add(label);
                  }
  public Dimension getPreferredSize()
        {return new Dimension(200,50);}
                               }
  public class Example5_7 extends Applet implements ActionListener{
        CardLayout mycard;MyPanel myPanel[];JPanel p;
        private void addButton(JPanel pan,String butName,ActionListener listener){
               JButton aButton=new JButton(butName);
               aButton.setActionCommand(butName);
               aButton.addActionListener(listener);
               pan.add(aButton);
                                                                                  }
        public void init(){
               setLayout(new BorderLayout());
               mycard=new CardLayout();
               this.setSize(400,150);//1这个this和下面的注释2-4中的是指一个对象吗?好像不是吧。那是指谁呢?
               p=new JPanel();p.setLayout(mycard);
               myPanel=new MyPanel[10];
               for(int i=0;i<10;i++){
                   myPanel[i]=new MyPanel(i+1);
                   p.add("A"+i,myPanel[i]);
                                    }
       JPanel p2=new JPanel();
       addButton(p2,"第一个",this);//2按addButton的的方法看,这个this是指注册的监听器吗?
       addButton(p2,"最后一个",this);//3同2
       addButton(p2,"前一个",this);//4同2
       addButton(p2,"后一个",this);//5同2
       add(p,"Center");add(p2,"South");
                           }
       public void actionPerformed(ActionEvent e){
              if(e.getActionCommand().equals("第一个"))mycard.first(p);
              else if(e.getActionCommand().equals("最后一个"))mycard.last(p);
              else if(e.getActionCommand().equals("前一个"))mycard.previous(p);
              else if(e.getActionCommand().equals("后一个"))mycard.next(p);
                                                  }
                                                                        }

解决方案 »

  1.   

    都是同一个this,都是指向本身这个对象.
      

  2.   

    如果你知道 
    public class a
    {
     int a
      public a(int a)
     {
        this.a = a;
     }
    的意思,你就好理解了, 这里的this指的是 全局变量 a
    说,你的代码里的This 指的是 这个对象自己。
      

  3.   

    是一个对象,this == 调用你的方法那个对象,
      

  4.   

    还是不明白你们说的“这个对象本身”是谁?按二楼的说法“这个对象本身”是指类Example5_7吗?
      

  5.   

    也就是说是指由类Example5_7创建的小应用程序的窗口?
      

  6.   

    this就是指当前类的对象。public class Student
    {
    private String name;public Student(String name)
    {
    this.name=name;
    }public void display()
    {
    this.show();
    }public void show()
    {
    System.out.println("姓名:"+this.name);
    }
    }注意如果声明为静态的类或者属性,就不能用this管间字
      

  7.   

    Java中的this随处可见,用法也很多,主要有以下几点:
    1. this是指当前对象自己本身。 
    当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用。如下面这个例子中: 
      public class Hello { 
     
         String s = "Hello"; 
     
          public Hello(String s){ 
             System.out.println("s = " + s); 
             System.out.println("1 -> this.s = " + this.s); 
             this.s = s; 
             System.out.println("2 -> this.s = " + this.s); 
         } 
     
          public static void main(String[] args) { 
         Hello x=new Hello("HelloWorld!"); 
         } 
     }
     
     
    运行结果: 
     s = HelloWorld! 
     1 -> this.s = Hello 
     2 -> this.s = HelloWorld!
     
    在这个例子中,构造函数Hello中,参数s与类Hello的变量s同名,这时如果直接对s进行操作则是对参数s进行操作。若要对类Hello的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印结果; 第二行是对成员变量s的打印;第三行是先对成员变量s赋传过来的参数s值后再打印,所以结果是HelloWorld!
    2. 把this作为参数传递 
    当你要把自己作为参数传递给别的对象时,也可以用this。如: 
      public class A { 
        public A() { 
         new B(this).print(); 
       } 
     
        public void print() { 
         System.out.println("Hello from A!"); 
       } 
     } 
     
      public class B { 
       A a; 
        public B(A a) { 
         this.a = a; 
       } 
     
        public void print() { 
         a.print(); 
         System.out.println("Hello from B!"); 
       } 
     } 
     
     运行结果: 
       Hello from A! 
       Hello from B! 
     
     
    在这个例子中,对象A的构造函数中,用new B(this)把对象A自己作为参数传递给了对象B的构造函数。 
     
    3. 注意匿名类和内部类中的中的this。 
    有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿名类中用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。如下面这个例子: 
      public class A { 
         int i = 1; 
     
          public A() { 
              Thread thread = new Thread() { 
                  public void run() { 
                      for(;;) { 
                         A.this.run(); 
                          try { 
                             sleep(1000); 
                          } catch(InterruptedException ie) {     }
     
                     } 
                } 
             }; //注意这里有; 
             thread.start(); 
         } 
     
          public void run() { 
             System.out.println("i = " + i); 
             i++; 
         } 
     
          public static void main(String[] args) throws Exception { 
             new A(); 
         }
     } 
     在上面这个例子中, thread 是一个匿名类对象,在它的定义中,它的 run 函数里用到了外部类的 run 函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的 run 函数换一个名字,但这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。 
     
    4。在构造函数中,通过this可以调用同一class中别的构造函数,如
      public class Flower{ 
                 Flower (int petals){} 
                 Flower(String ss){} 
                 Flower(int petals, Sting ss){ 
                     //petals++;调用另一个构造函数的语句必须在最起始的位置 
                     this(petals); 
                    //this(ss);会产生错误,因为在一个构造函数中只能调用一个构造函数 
                } 
     } 
     
    值得注意的是:
    1:在构造调用另一个构造函数,调用动作必须置于最起始的位置。
    2:不能在构造函数以外的任何函数内调用构造函数。
    3:在一个构造函数内只能调用一个构造函数