ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

This和Super关键字的对比

2021-01-19 13:34:03  阅读:106  来源: 互联网

标签:name rollno 关键字 Student 构造函数 Super 对比 String



this和Super关键字

this和Super关键字的对比

-thissuper()
概念访问本类实列属性和方法有子类访问父类中的实列属性和方法
查找范围先找本类,找不到再找父类找父类
各异功能单独使用时,表示当前对象在子类重写父类方法时,访问父类同名方法
相同点1.都是关键字,起指代作用2.在构造方法中必须出现在第一行

Super关键字的用法如下:

1. super关键字代表了父类空间的引用;

2. super关键字的作用:

(1) 子父类存在着同名的成员(包括变量和方法)时,在子类中默认是访问子类的成员,可以通过super关键字指定访问父类的成员;
(2) 创建子类对象时,默认会先调用父类无参的构造方法,可以通过super关键字指定调用父类的构造方法。
(1) 如果在子类的构造方法上没有指定调用父类的构造方法,那么java编译器会在子类的构造方法内加上super()语句。
(2) super关键字调用父类的构造函数时,该语句必须要是子类构造函数中的第一个语句。
(3) super与this关键字不能同时出现在同一个构造函数中调用其他的构造函数。因为两个语句都需要第一个语句。
(1) 代表的事物不一致。
① super关键字代表的是父类空间的引用。(并不能代表对象,只是代表一个对象中的一块内存而已)
② this关键字代表的是所属函数的调用者对象。
(2) 使用前提不一致。
① super关键字必须要有继承关系才能使用。
② this关键字不需要存在继承关系也可使用。
(3) 调用构造函数的区别:
① super关键字是调用父类的构造函数。
② this关键字是调用本类的构造函数。

3. super关键字调用父类构造方法要注意的事项:

注意:是两个关键字不能同时出现在同一个构造函数中去调用其他的构造函数,里面还是可以写this.num = num。并不是说不能出现this。

this关键字的用法如下:

  1. this关键字可用来引用当前类的实例变量。
  2. this关键字可用于调用当前类方法(隐式)。
  3. this()可以用来调用当前类的构造函数。
  4. this关键字可作为调用方法中的参数传递。
  5. this关键字可作为参数在构造函数调用中传递。
  6. this关键字可用于从方法返回当前类的实例。
    建议:如果你是java初学者,只学习 this 关键字的前三个用法就可以了。
  1. this:引用当前类的实例变量 this关键字可以用来引用当前类的实例变量。如果实例变量和参数之间存在歧义,则 this 关键字可用于明确地指定类变量以解决歧义问题。

1.了解没有 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();
    }
}

执行上面代码输出结果如下 -

0 null 0.0
0 null 0.0

在上面的例子中,参数(形式参数)和实例变量(rollno和name)是相同的。 所以要使用this关键字来区分局部变量和实例变量。

使用 this 关键字解决了上面的问题

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

执行上面代码输出结果如下 -

111 ankit 5000
112 sumit 6000

如果局部变量(形式参数)和实例变量不同,则不需要像下面的程序一样使用this关键字:

2. 不需要 this 关键字的程序示例

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关键字。

3. this:调用当前类方法

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

执行上面代码输出结果如下 -

hello n
hello m

3. 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

4. 使用this()构造函数调用

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()必须是构造函数中的第一个语句。

下面示例为不把 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();
    }
}

执行上面代码输出结果如下 -

Compile Time Error: Call to this must be first statement in constructor

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

执行上面代码输出结果如下 -

method is invoked

这个应用程序可以作为参数传递:

在事件处理(或)的情况下,必须提供一个类的引用到另一个。 它用于在多个方法中重用一个对象。

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

执行上面代码输出结果如下 -

10

6. this关键字用来返回当前类的实例

可以从方法中 this 关键字作为语句返回。 在这种情况下,方法的返回类型必须是类类型(非原始)。 看看下面的一个例子:

作为语句返回的语法

return_type method_name(){  
    return this;  
}

从方法中返回为语句的 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

7. 验证 this 关键字

现在来验证 this 关键字引用当前类的实例变量。 在这个程序中将打印参考变量,这两个变量的输出是相同的。

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

标签:name,rollno,关键字,Student,构造函数,Super,对比,String
来源: https://blog.csdn.net/qq_41704415/article/details/112802648

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有