zoukankan      html  css  js  c++  java
  • 从头认识java-4.4 this

    这一章节我们来讨论一些this

    1 this是在类的内部使用,它指向对象的引用。

    package com.ray.ch01;
    
    public class Test {
    
    	private Test getTest() {
    		return this;
    	}
    
    	public static void main(String[] args) {
    	}
    }
    

    上面代码里面的this就是返回Test这个类型的对象。

    this还可以操作方法:

    package com.ray.ch01;
    
    public class Test {
    
    	private void say() {
    		System.out.println("method say");
    	}
    
    	private void testSay() {
    		this.say();
    	}
    
    	public static void main(String[] args) {
    		new Test().testSay();
    	}
    }
    
    输出:

    method say

    上面的代码通过引用Test的对象,操作say这个方法。


    其实,在类的内部使不使用this倒无所谓,只不过增加了可读性,下面的代码为我们展示这个:

    package com.ray.ch01;
    
    public class Test {
    
    	private void say() {
    		System.out.println("method say");
    	}
    
    	private void testSay1() {
    		this.say();
    	}
    
    	private void testSay2() {
    		say();
    	}
    
    	public static void main(String[] args) {
    		new Test().testSay1();
    		new Test().testSay2();
    	}
    }
    

    输出:

    method say

    method say

    从输出结果可以看出,其实有没有this,都是可以调用内部方法。


    2 this可以更好区分参数与属性域。

    我们在编码的时候,使用很多参数的名称都跟属性域的相同,因此,使用this让我们更好的区分两者。

    例如:

    package com.ray.ch01;
    
    public class Test {
    
    	private int id;
    
    	public Test(int id) {
    		id = id;
    	}
    
    	public static void main(String[] args) {
    	}
    }
    

    上面的代码非常难区分两个id究竟赋值是怎样的,因此,我们在这里加上this,让代码更好理解。

    package com.ray.ch01;
    
    public class Test {
    
    	private int id;
    
    	public Test(int id) {
    		this.id = id;
    	}
    
    	public static void main(String[] args) {
    	}
    }
    

    3 this可以引用之前的构造器。

    package com.ray.ch01;
    
    public class Test {
    
    	private int id;
    
    	private String name;
    
    	public Test() {
    		System.out.println("created Test");
    	}
    
    	public Test(int id) {
    		this();
    		this.id = id;
    		System.out.println("id:" + id);
    	}
    
    	public Test(int id, String name) {
    		this(id);
    		this.name = name;
    		System.out.println("name:" + name);
    	}
    
    	public static void main(String[] args) {
    		new Test(1, "jack");
    	}
    }
    

    输出:

    created Test
    id:1
    name:jack

    由于使用了this,因此使得代码更加间接。

    注意:

    (1)this()引用的构造器必须放在第一句,不然会报错。

    (2)this()引用的构造器不能同时使用两个以上。

    (3)this只能引用普通方法,而this()只能引用构造器方法。


    4 this不能引用static方法

    package com.ray.ch01;
    
    public class Test {
    
    	private static void say() {
    		System.out.println("method say");
    	}
    
    	public static void main(String[] args) {
    		Test.say();
    	}
    }
    

    有static的方法是类方法,它不需要创建对象即可使用,但是这种方法不能够被this引用。


    总结:这一章节我们主要讨论了this的四个注意点,以及注意点在编码中的实现。


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
    nyoj 1185 最大最小值【线段树最大值最小值维护】
    nyoj 123 士兵杀敌(四) 树状数组【单点查询+区间修改】
    poj 3468 A Simple Problem with Integers【线段树区间修改】
    hdoj 1698 Just a Hook【线段树区间修改】
    hdoj 1556 Color the ball【线段树区间更新】
    hdoj 1286 找新朋友【欧拉函数】
    [LC] 303. Range Sum Query
    [LC] 79. Word Search
    [LC] 211. Add and Search Word
  • 原文地址:https://www.cnblogs.com/raylee2007/p/4944476.html
Copyright © 2011-2022 走看看