专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

java中this的N种使用方法

  this可能是几乎所有有一点面向对象思想的语言都会引用到的变量,java自然不例外。只是,this有多少种用法,我也不知道了,让我们来see see。

由简入奢! 易。

来个例子说明下:

public class DebugerTest {

    public static void main(String[] args) {
        UserExample samp1 = new UserExample("amy");
        System.out.println("who are u? ");
        System.out.println(samp1.whoAreU());
        System.out.println("intro yourself?");
        System.out.println(samp1.introYourself());
    }

}

class UserExample {

    private String name;

    private Integer age;

    private MyDoll myDoll;

    public UserExample() {
        this(null);
    }

    // 3. 调用本类的其他构造方法
    public UserExample(String name) {
        this(name, -1);
    }

    public UserExample(String name, Integer age) {
        this.name = name;
        this.age = age;
        this.myDoll = new MyDoll("prize");
    }

    // 2. 调用本类属性
    public void changeMyName(String name) {
        this.name = name;
    }

    public void changeMyAge(Integer age) {
        this.age = age;
    }

    public String whoAreU() {
        return "I am " + name + ". ";
    }

    public String haoOldAreU() {
        return "i am " + age + " old.";
    }

    // 1. 调用本类方法
    public String introYourself() {
        return this.whoAreU() +
                this.haoOldAreU() +
                "\r\n whoAmI@ " + this.myDoll.whoAmI() +
                "\r\n whoAreSuper@ " + this.myDoll.whoAreSuper();
    }

    class MyDoll {

        private String name;

        public MyDoll(String name) {
            this.name = name;
        }

        public void changeMyName(String name) {
            this.name = name;
        }

        // 5. 隐藏式的调用
        public String whoAmI() {
            return whoAreU();
        }

        public String whoAreU() {
            return "I am a Doll, my name is " + name + ". ";
        }

        // 4. 调用父类的或指定的其他的类的同名方法
        public String whoAreSuper() {
            return "super is " + UserExample.this.whoAreU() + ". ";
        }

    }
}

1、 调用本类方法,表达更清晰

    public String introYourself() {
        return this.whoAreU() + this.haoOldAreU();
    }

2、 调用本类属性,基本功亮出来

    public void changeMyName(String name) {
        this.name = name;
    }

3、 调用本类的其他构造方法,更灵活

    public UserExample(String name) {
        this(name, -1);
    }

4、 调用父类的或指定的其他的类的同名方法,为避免歧义而生的方法

        public String whoAreSuper() {
            return "super is " + UserExample.this.whoAreU() + ". ";
        }

5、 隐藏式的调用,为了写代码方便(更常用),不指定范围,java会在全类范围内向上查找变量或方法

        public String whoAmI() {
            return whoAreU();
        }

以上样例输出结果如下所示:

101_1.png

  this是个基本的关键字,我们平时也一直在用,只是也不一定所有同学都清楚怎么用。
  越是基础的,越是厉害的!

文章永久链接:https://tech.souyunku.com/32440

未经允许不得转载:搜云库技术团队 » java中this的N种使用方法

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们