• 关于Java的this关键字


    java中的this随处可见,用法也多,现在整理有几点:
    1. this是指当前对象自己。

    当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用。如下面这个例子中:

    public class This_Demo {
    
        String s = "Hello";
    
        public This_Demo(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) {
            This_Demo demo = new This_Demo("Hello World");
        }
    }

    运行结果:

    s = Hello World
    1 -> this.s = Hello
    2 -> this.s = Hello World

         在这个例子中,构造函数This_Demo中,参数s与类This_Demo的变量s同名,这时如果直接对s进行操作则是对参数s进行操作。若要对类This_Demo的成员变量s进行操作就应该用this进行引用。运行结果的第一行就是直接对构造函数中传递过来的参数s进行打印的结果;第二行是对成员变量s的打印;第三行是先对成员变量s赋传过来的参数s值后再打印,所以得到第三行的打印结果。

    2.把this作为参数传递

    当你要把自己作为参数传递给别的对象时,也可以用this。如:

    class Person {
    	public void eat(Apple apple) {
    		Apple peeled = apple.getPeeled();
    		System.out.println("Yummy");
    	}
    }
    
    class Peeler {
    	static Apple peel(Apple apple) {
    		return apple;
    	}
    }
    
    class Apple {
    	Apple getPeeled() {
    		return Peeler.peel(this);
    	}
    }
    
    public class PassingThis {
    	public static void main(String[] args) {
    		new Person().eat(new Apple());
    	}
    }
    运行结果:
    Yummy
         Apple需要调用Peeler.peel()方法,它是一个外部的工具方法,将执行由于某种原因而必须放在Apple外部的操作,为了将其自身传递给外部方法,Apple必须使用this关键字。
    3.注意匿名类和内部类中的中的this。
    有时候,我们会用到一些内部类和匿名类,如事件处理。当在匿名类中使用this时,这个this则指的是匿名类或内部类本身。这时如果我们要使用外部类的方法和变量的话,则应该加上外部类的类名。如下面这个例子:
    public class This_Inner {
    	int i = 1;
    	public This_Inner() {
    		Thread thread = new Thread() {
    			@Override
    			public void run() {
    				for(;;) {
    					This_Inner.this.run();
    					try {
    						sleep(1000);
    					} catch (Exception e) {
    						
    					}
    				}
    			}
    		};
    		thread.start();
    	}
    	public void run() {
    		System.out.println("i = " + i);
    		i++;
    	}
    	public static void main(String[] args) {
    		new This_Inner();
    	}
    }
    运行结果:

    i = 1
    i = 2
    i = 3

         在上面这个例子中,thread是一个匿名类对象,在它的定义中,它的run函数里用到了外部类的run函数。这时由于函数同名,直接调用就不行了。这时有两种办法,一种就是把外部的run函数换一个名字,但是这种办法对于一个开发到中途的应用来说是不可取的。那么就可以用这个例子中的办法用外部类的类名加上 this 引用来说明要调用的是外部类的方法 run。

    4.在构造器中调用构造器

    可能为一个类写了多个构造器,有时可能想在一个构造器中调用另一个构造器,以避免重复代码。可用this关键字做到这一点。

    通常写this的时候,都是指“这个对象”或者“当前对象”,而且它本身表示对当前对象的引用。在构造器中,如果为this添加了参数列表,那么就有了不同的含义。这将产生对符合此参数列表的某个构造器的明确调用;这样,调用其他构造器就有了直接的途径:

    public class Flower {
    	int petalCout = 0;
    	String s = "initial value";
    	public Flower(int petals) {
    		petalCout = petals;
    		System.out.println("Constructor w/ int arg only, petalCount = " + petalCout);
    	}
    	public Flower(String ss) {
    		System.out.println("Constructor w/ String arg only, s = " + ss);
    	}
    	public Flower(String s, int petals) {
    		this(petals);
    //!		this(s);   不能调用两个
    		System.out.println("String & int args");
    	}
    	public Flower() {
    		this("hi",88);
    		System.out.println("default constructor (no args)");
    	}
    	void printPetalCount() {
    //!		this(11);	//不是在非构造函数里
    		System.out.println("petalCount = " + petalCout + "...s = " + s);
    	}
    	public static void main(String[] args) {
    		Flower x = new Flower();
    		x.printPetalCount();
    	}
    }

    运行结果:

    Constructor w/ int arg only, petalCount = 88
    String & int args
    default constructor (no args)
    petalCount = 88...s = initial value

         构造器Flower(String s, int petals)表明:尽管可以用this调用一个构造器,但却不能调用两个。此外,必须将构造器调用至于最起始处,否则编译器会报错。

         这个例子也展示了this的另一种用法(该用法在第一点已经提过)。由于参数s的名称和数据成员s的名称相同,所以会产生歧义。使用this.s来代表数据成员就可以解决这个问题。

  • 相关阅读:
    C# 文字转声音
    Parameter配置文件获取
    反射动态创建不同的Processor
    程序代码记Log
    Dictionary序列化和反序列化
    HQueue:基于HBase的消息队列
    Angular系列----AngularJS入门教程05:双向绑定(转载)
    Angular系列----AngularJS入门教程04:迭代器过滤(转载)
    Angular系列---- AngularJS入门教程03:AngularJS 模板(转载)
    Angular系列----AngularJS入门教程02:静态模板(转载)
  • 原文地址:https://www.cnblogs.com/whyalwaysme/p/4361046.html
Copyright © 2020-2023  润新知