java中的this关键字

每日福利

image

this 关键字可以有很多用法。在java中,这是一个引用当前对象的引用变量。

image.png

使用java这个关键字

这里给出了java这个关键字的6个用法。

1. 这可以用来引用当前的类实例变量。

2. 这可以用来调用当前的类方法(隐式)

3. this()可用于调用当前类构造函数。

4. 这可以作为方法调用中的参数传递。

5. 这可以在构造函数调用中作为参数传递。

6. 这可以用于从方法返回当前类实例。

建议:如果您是初学者,请仅查找此关键字的三种用法。

image.png

this:引用当前类的实例变量

this关键字可用于引用当前的类实例变量。如果实例变量和参数之间存在歧义,则此关键字可解决歧义问题。

没有此关键字了解问题

如果我们不通过下面给出的示例使用此关键字,请理解该问题:


    class Student {

        int rollno;

        String name;

        float fee;

        Student(int rollno, String name, float fee) {

            rollno = rollno;

            name = name;

            fee = fee;

        }

        void display() {

            System.out.println(rollno + " " + name + " " + fee);

        }

    }

    class TestThis1 {

        public static void main(String args[]) {

            Student s1 = new Student(111, "ankit", 5000f);

            Student s2 = new Student(112, "sumit", 6000f);

            s1.display();

            s2.display();

        }

    }

输出结果:


 <pre>0 null 0.0
0 null 0.0</pre> 

在上面的例子中,参数(形式参数)和实例变量是相同的。因此,我们使用此关键字来区分局部变量和实例变量。

通过此关键字解决上述问题


    class Student {

        int rollno;

        String name;

        float fee;

        Student(int rollno, String name, float fee) {

            this.rollno = rollno;

            this.name = name;

            this.fee = fee;

        }

        void display() {

            System.out.println(rollno + " " + name + " " + fee);

        }

    }

    class TestThis2 {

        public static void main(String args[]) {

            Student s1 = new Student(111, "ankit", 5000f);

            Student s2 = new Student(112, "sumit", 6000f);

            s1.display();

            s2.display();

        }

    }

输出结果:


 <pre>111 ankit 5000
112 sumit 6000</pre> 

如果局部变量(形式参数)和实例变量不同,则无需像以下程序那样使用此关键字:

不需要此关键字的程序


    class Student {

        int rollno;

        String name;

        float fee;

        Student(int r, String n, float f) {

            rollno = r;

            name = n;

            fee = f;

        }

        void display() {

            System.out.println(rollno + " " + name + " " + fee);

        }

    }

    class TestThis3 {

        public static void main(String args[]) {

            Student s1 = new Student(111, "ankit", 5000f);

            Student s2 = new Student(112, "sumit", 6000f);

            s1.display();

            s2.display();

        }

    }

输出结果:


111 ankit 5000

112 sumit 6000

对变量使用有意义的名称是更好的方法。因此我们实时使用相同的名称作为实例变量和参数,并始终使用此关键字。

this:调用当前的类方法

您可以使用this关键字调用当前类的方法。如果不使用this关键字,编译器会在调用方法时自动添加此关键字。我们来看看这个例子

[图片上传失败…(image-7cbf51-1546565796978)]


    class A {

        void m() {

            System.out.println("hello m");

        }

        void n() {

            System.out.println("hello n");

            //m();//same as this.m()  

            this.m();

        }

    }

    class TestThis4 {

        public static void main(String args[]) {

            A a = new A();

            a.n();

        }

    }

输出结果:


hello n

hello m

this():调用当前的类构造函数

this()构造函数调用可用于调用当前的类构造函数。它用于重用构造函数。换句话说,它用于构造函数链接。

从参数化构造函数中调用默认构造函数:


    class A {

        A() {

            System.out.println("hello a");

        }

        A(int x) {

            this();

            System.out.println(x);

        }

    }

    class TestThis5 {

        public static void main(String args[]) {

            A a = new A(10);

        }

    }

输出结果:


hello a

10

从默认构造函数调用参数化构造函数:


    class A {

        A() {

            this(5);

            System.out.println("hello a");

        }

        A(int x) {

            System.out.println(x);

        }

    }

    class TestThis6 {

        public static void main(String args[]) {

            A a = new A();

        }

    }

输出结果:


5

hello a

this()构造函数调用的实际用法

应该使用this()构造函数调用来重用构造函数中的构造函数。它维护构造函数之间的链,即它用于构造函数链。让我们看一下下面给出的例子,它显示了这个关键字的实际用途。


    class Student {

        int rollno;

        String name, course;

        float fee;

        Student(int rollno, String name, String course) {

            this.rollno = rollno;

            this.name = name;

            this.course = course;

        }

        Student(int rollno, String name, String course, float fee) {

            this(rollno, name, course);//reusing constructor  

            this.fee = fee;

        }

        void display() {

            System.out.println(rollno + " " + name + " " + course + " " + fee);

        }

    }

    class TestThis7 {

        public static void main(String args[]) {

            Student s1 = new Student(111, "ankit", "java");

            Student s2 = new Student(112, "sumit", "java", 6000f);

            s1.display();

            s2.display();

        }

    }

输出结果:


111 ankit java null

112 sumit java 6000

规则:调用this()必须是构造函数中的第一个语句。


    class Student {

        int rollno;

        String name, course;

        float fee;

        Student(int rollno, String name, String course) {

            this.rollno = rollno;

            this.name = name;

            this.course = course;

        }

        Student(int rollno, String name, String course, float fee) {

            this.fee = fee;

            this(rollno, name, course);//C.T.Error  

        }

        void display() {

            System.out.println(rollno + " " + name + " " + course + " " + fee);

        }

    }

    class TestThis8 {

        public static void main(String args[]) {

            Student s1 = new Student(111, "ankit", "java");

            Student s2 = new Student(112, "sumit", "java", 6000f);

            s1.display();

            s2.display();

        }

    }

编译时错误:调用此必须是构造函数中的第一个语句

this:在方法中作为参数传递

this关键字也可以作为方法中的参数传递。它主要用于事件处理。我们来看看这个例子:


    class S2 {

        void m(S2 obj) {

            System.out.println("method is invoked");

        }

        void p() {

            m(this);

        }

        public static void main(String args[]) {

            S2 s1 = new S2();

            s1.p();

        }

    }

输出结果:


 <pre>method is invoked</pre> 

可以作为参数传递的应用:

在事件处理(或)中,我们必须将类的引用提供给另一个类。它用于在许多方法中重用一个对象。

this:在构造函数调用中作为参数传递

我们也可以在构造函数中传递this关键字。如果我们必须在多个类中使用一个对象,这将非常有用。我们来看看这个例子:


    class B {

        A4 obj;

        B(A4 obj) {

            this.obj = obj;

        }

        void display() {

            System.out.println(obj.data);//using data member of A4 class  

        }

    }

    class A4 {

        int data = 10;

        A4() {

            B b = new B(this);

            b.display();

        }

        public static void main(String args[]) {

            A4 a = new A4();

        }

    }


Output:10

此关键字可用于返回当前的类实例

我们可以将此关键字作为该方法的语句返回。在这种情况下,方法的返回类型必须是类类型(非基元)。我们来看看这个例子:

可以作为语句返回的语法


return_type method_name(){

    return this;

}

作为方法中的语句返回的此关键字的示例


   class A {

        A getA() {

            return this;

        }

        void msg() {

            System.out.println("Hello java");

        }

    }

    class Test1 {

        public static void main(String args[]) {

            new A().getA().msg();

        }

    }

输出:


Hello java

证明这个关键字

让我们证明这个关键字引用了当前的类实例变量。在这个程序中,我们打印引用变量,这两个变量的输出是相同的。


    class A5{

        void m(){

            System.out.println(this);//prints same reference ID  

        }

        public static void main(String args[]){

            A5 obj=new A5();

            System.out.println(obj);//prints the reference ID  

            obj.m();

        }

    }

输出:


A5@22b3ea59

A5@22b3ea59

每日福利

image

微信关注:JAVA知己,每天更新。

JAVA知己
image

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值