• Java之this详解


    1. this是指当前对象自己。

    用类名定义一个变量的时候,定义的应该只是一个引用,外面可以通过这个引用来访问这个类里面的属性和方法,那们类里面是够也应该有一个引用来访问自己的属性和方法纳?呵呵,JAVA提供了一个很好的东西,就是 this 对象,它可以在类里面来引用这个类的属性和方法。如下面这个例子中:

    public class ThisDemo {
    	String name = "Mick";
    
    	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("Orson");
    	}
    }
    

    2.使用this区分同名变量

    成员变量与方法内部的变量重名时,希望在方法内部调用成员变量,怎么办呢?这时候只能使用this,例如:

    public class Demo {
    	public String name;
    	public int age;
    
    	public Demo(String name, int age) {
    		this.name = name;
    		this.age = age;
    	}
    
    	public void say() {
    		System.out.println(name + " " + age);
    	}
    
    	public static void main(String[] args) {
    		Demo obj = new Demo("hdk1993", 21);
    		obj.say();
    	}
    }

    3.使用this调用构造函数

    在构造函数中,通过this可以调用同一class中别的构造函数,如

    public class Flower {
    	Flower(int petals) {
    	}
    
    	Flower(String ss) {
    	}
    
    	Flower(int petals, Sting ss) {
    		// petals++;调用另一个构造函数的语句必须在最起始的位置
    		this(petals);
    		// this(ss);会产生错误,因为在一个构造函数中只能调用一个构造函数
    	}
    }

    值得注意的是:

    (i):在构造调用另一个构造函数,调用动作必须置于最起始的位置。

    (ii):不能在构造函数以外的任何函数内调用构造函数。

    (iii):在一个构造函数内只能调用一个构造函数。

    4.this作为函数返回值

    关于返回类自身的引用,Thing in Java有个很经典的例子,通过this 这个关键字返回自身这个对象然后在一条语句里面实现多次的操作,还是贴出来。

    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();
    
    	}
    
    }
    

    5.this作为参数传递

    需要在某些完全分离的类中调用一个方法,并将当前对象的一个引用作为参数传递时。例如:

    public class Demo {
    
    	public static void main(String[] args) {
    
    		B b = new B(new A());
    
    	}
    
    }
    
    class A {
    
    	public A() {
    
    		new B(this).print(); // 匿名对象
    
    	}
    
    	public void print() {
    
    		System.out.println("Hello from A!");
    
    	}
    
    }
    
    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的构造函数。
    匿名对象就是没有名字的对象。如果对象只使用一次,就可以作为匿名对象,代码中 new B(this).print(); 等价于 ( new B(this) ).print();,先通过 new B(this) 创建一个没有名字的对象,再调用它的方法。

    6. 注意匿名类和内部类中的中的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();
    	}
    }
    

    clip_image009[1]

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

  • 相关阅读:
    ansible笔记(11):初识ansible playbook(二)
    Linux下查看占用CPU与内存最高的进程
    ansible笔记(10):初识ansible playbook
    AbpZero Http 模式下 Chrome浏览器因Cookie 不能登录
    Tomcat 8443&8080 并存
    接入腾讯cos文件存储
    安卓包打渠道标签
    java Android与PHP encode的区别
    thinkphp常用
    phalcon task任务
  • 原文地址:https://www.cnblogs.com/hdk1993/p/4400958.html
Copyright © 2020-2023  润新知