zoukankan      html  css  js  c++  java
  • this的相关介绍与用法

      当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是this。因此,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this,并且this只和特定的对象关联,而不和类关联,同一个类的不同对象有不同的this。

    下面我们将通过一个小例子来了解一下this的相关用法与注意事项:

    package test;
    
    public class ThisTest {
    
        private int i=0;
    
        //第一个构造器:有一个int型形参
    
        ThisTest(int i){
    
           this.i=i+1;//此时this表示引用成员变量i,而非函数参数i
    
           System.out.println("Int constructor i——this.i:  "+i+"——"+this.i);
    
           System.out.println("i-1:"+(i-1)+"this.i+1:"+(this.i+1));
    
           //从两个输出结果充分证明了i和this.i是不一样的!
    
        }
    
        //  第二个构造器:有一个String型形参
    
        ThisTest(String s){
    
           System.out.println("String constructor:  "+s);
    
        }
    
        //  第三个构造器:有一个int型形参和一个String型形参
    
        ThisTest(int i,String s){
    
           this(s);//this调用第二个构造器
    
           //this(i); this调用第一个构造器
    
          
    
           this.i=i++;//this以引用该类的成员变量
    
           System.out.println("Int constructor:  "+i+"/n"+"String constructor:  "+s);
    
        }
    
        public ThisTest increment(){
    
           this.i++;
    
           return this;//返回的是当前的对象,该对象属于(ThisTest)
    
        }
    
        public static void main(String[] args){
    
           ThisTest tt0=new ThisTest(10);
    
           ThisTest tt1=new ThisTest("ok");
    
           ThisTest tt2=new ThisTest(20,"ok again!");
    
          
    
           System.out.println(tt0.increment().increment().increment().i);
    
           //tt0.increment()返回一个在tt0基础上i++的ThisTest对象,
    
           //接着又返回在上面返回的对象基础上i++的ThisTest对象!
    
        }
    
    }

    运行结果:

    Int constructor i——this.i:  10——11

    String constructor:  ok

    String constructor:  ok again!

    Int constructor:  21

    String constructor:  ok again!

    14

     

    总结一下,this主要注意事项:

    1. 表示用类的成员变量,而非函数参数,注意在函数参数和成员变量同名是进行区分。
    2. 用于在构造方法中引用满足指定参数类型的构造器时必须非常注意:一个构造器中只能有一个this且必须在首位
    3. this不能用在static方法中!(具体原因参见文首)

    this的主要用途:

    1. 通过this调用另一个构造方法,用法是this(参数列表),这个仅仅能在类的构造方法中使用,其他地方不行。
    2. 函数参数或者函数中的局部变量和成员变量同名的情况下,成员变量被屏蔽,此时要访问成员变量则需要用“this.成员变量名”的方式来引用成员变量。当然,在没有同名的情况下,可以直接用成员变量的名字,但使用了this也是正确的。
    3. 当需要访问另一个类的成员变量或者方法时(非静态),除了通过内部类访问包装类以外。还能通过将要被访问的类的引用(this)传递出去,然后在要进行访问操作的类中将其接受并作为成员变量储存起来。随后便可通过该成员变量对另一个类进行访问(该成员变量实际上就是一个指向被访问类的指针)。 (可参考TextFieldPlus.java该程序)

    综上:这些总结都是由“this是指向对象本身的一个指针”这句话的更深入的理解而来的。

     

    附录:

     

    /*
       TextFieldPlus
       author: cherryljr
    */
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class TextFieldPlus {
        public static void main(String[] args) {
            new MyFrame();
        }
    }
    
    class MyFrame extends Frame {
        TextField num1, num2, num3;
        
        MyFrame() {
            super("PlusTest");
            num1 = new TextField(10);
            num2 = new TextField(10);
            num3 = new TextField(10);
            Label plus = new Label("+");
            Button equl = new Button("=");
            equl.addActionListener(new Monitor(this));  //  将当前对象的引用传递给Monitor
            
            setLayout(new FlowLayout());
            add(num1);
            add(plus);
            add(num2);
            add(equl);
            add(num3);
            pack();
            setVisible(true);
        }
    }
    
    class Monitor implements ActionListener {
        MyFrame f = null;     
         //  还可以通过内部类实现,即无需进行传递直接调用包装类(MyFrame)的成员
        Monitor(MyFrame f) {
            this.f = f;   //  重写Monitor的构造方法,接受并储存传递过来的指针以便使用
        }
        public void actionPerformed(ActionEvent e) {
            int n1 = Integer.parseInt(f.num1.getText());
            int n2 = Integer.parseInt(f.num2.getText());
            f.num3.setText("" + (n1 + n2));
        }
    }        

     

    《Thinking in java》上两个关于this使用的小程序:

    public class ThisDemo {  
        String name = "cherry";
        public void print(String name){
            System.out.println("类中的属性 name =" + this.name);
            System.out.println("局部传参的属性 =" + name);
        }   
        public static void main(String[] args) {
            ThisDemo tt = new ThisDemo();
            tt.print("sakura");
        }
    }
    public class ThisDemo {  
        int number;
        ThisDemo increment(){
             number++;
             return this;
        }  
      private void print(){
             System.out.println("number=" + number);
        }
        public static void main(String[] args) {
            ThisDemo tt=new ThisDemo();
             tt.increment().increment().increment().print();
        }
    }
  • 相关阅读:
    ssh无密码登录 及集群分发脚本
    Hbase安装及客户端测试及笔记
    Zookeeper 的分布式安装部署及客户端测试
    搭建hadoop-完全分布式模式
    虚拟机的linux没有mac地址
    servlet-Listener 的复习
    纯servlet 达到spring的自动装配
    JDBC的复习 和工具类总结
    C#中的弱事件(Weak Events in C#)
    WeakReference(弱引用)
  • 原文地址:https://www.cnblogs.com/cherryljr/p/6390998.html
Copyright © 2011-2022 走看看