JAVA中this的作用

JAVA中this的作用

this的用法
1.当成员变量和局部变量重名时,在方法中使用this时,表示的是该方法所在类中的成员变量(this是当前对象自己)。

public class Demo1 {

    String s="hello";

    public static void main(String[] args) {
        Demo1 demo1=new Demo1("bye");
        //验证成员变量值的改变
        System.out.println(demo1.s);
    }

    public Demo1(String s) {
        System.out.println("s="+s);
        //此时this.s表示方法所在的类的成员变量,就是hello
        System.out.println("1.this.s="+this.s);
        //把参数赋予成员变量,成员变量的值改变
        this.s = s;
        System.out.println("2.this.s="+this.s);
    }

}

2.把自己当作参数传递时,也可以用this(htis作当前参数进行传递)

public class Demo2 {

     class A {
         //A的构造函数,用new B(this)把A自己作为参数
        public A() {
            new B(this).print();
        }

        public void print() {
            System.out.println("hello a from a");
        }
    }

    class B {
        A a;

        public B(A a) {
            this.a = a;
        }

        public void print() {
            a.print();
            System.out.println("hello b from b");
        }
    }

    public void test(){
        A aa = new A();
        aa.print();
        B bb = new B(aa);
        bb.print();
    }

    public static void main(String[] args) {
         Demo2 demo2=new Demo2();
         demo2.test();
    }

}

3.在内部类和匿名类中,this指的是内部类和匿名类本身,如果我们要使用外部类的方法和变量时,需要加上外部类的类名。

public class Demo3 {

    int i=1;

    public Demo3(){
        Thread thread=new Thread(){
            @Override
            public void run() {
                for (int j = 0; j < 6; j++) {
                    //调用外部类的方法
                    //在内部类和匿名类中,this指的是内部类和匿名类本身,如果我们要使用外部类的方法和变量时,需要加上外部类的类名
                    Demo3.this.run();
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        thread.start();
    }

    public void run(){
        System.out.println("i="+i);
        i++;
    }

    public static void main(String[] args) {
        new Demo3();
    }

}

4.在构造函数中,通过this可以调用同一类中别的构造函数。
1)在构造函数中调用另一个构造函数,调用动作必须放在最开始的位置。由于super调用父类的构造函数也必须放在构造方法的第一行中执行,因此this和super调用构造方法不能同时出现在一个构造方法中。
2)不能在构造函数以外的任何函数调用构造函数。
3)一个构造函数最多只能调用一个构造函数。

public class Demo4 {

    private int age;
    private String str;

    public Demo4(String str) {
        this.str = str;
        System.out.println(str);
    }

    public Demo4(String str ,int age) {
        //在构造函数中调用另一个构造函数,调用动作必须放在最开始的位置。
        //不能在构造函数以外的任何函数调用构造函数
        //一个构造函数最多只能调用一个构造函数
        this(str);
        this.age = age;
        System.out.println(age);
    }

    public static void main(String[] args) {
        Demo4 demo4 = new Demo4("测试成功", 10);
    }
}

5.this同时传递多个参数。

public class Demo5 {
    int x=0;
    int y=0;

    //实例化对象
    static void test(Demo5 demo5) {
        System.out.println(demo5.x + "   " + demo5.y);
    }

    void seeit() {
        //把当前实例化的dd传给了test方法,从而运行。
        test(this);
    }

    public static void main(String[] args) {
        Demo5 dd = new Demo5();
        dd.x=1;
        dd.y=2;
        dd.seeit();
    }

}

整理借鉴了很多大佬写的,在此无法一一说明,这只是个人用来查漏补缺的文章,如果对你有帮助我很高兴。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值