super关键字用于访问 父类 而这是用来访问 当前类别 .
- 这 是java中的保留关键字,也就是说,我们不能将其用作标识符。
- 这 用来指代 当前类的实例以及静态成员。
这 可用于以下各种情况:
- 引用当前类的实例变量
- 调用或启动当前类构造函数
- 可以在方法调用中作为参数传递
- 可以在构造函数调用中作为参数传递
- 可用于返回当前类实例
JAVA
// Program to illustrate this keyword // is used to refer current class class RR { // instance variable int a = 10 ; // static variable static int b = 20 ; void GFG() { // referring current class(i.e, class RR) // instance variable(i.e, a) this .a = 100 ; System.out.println(a); // referring current class(i.e, class RR) // static variable(i.e, b) this .b = 600 ; System.out.println(b); } public static void main(String[] args) { // Uncomment this and see here you get // Compile Time Error since cannot use // 'this' in static context. // this.a = 700; new RR().GFG(); } } |
100600
超级的 关键词
- 超级的 是java中的保留关键字,也就是说,我们不能将其用作标识符。
- 超级的 用来指代 超级类的实例以及静态成员 .
- 超级的 也用于调用 超类的方法或构造函数 .
JAVA
// Program to illustrate super keyword // refers super-class instance class Parent { // instance variable int a = 10 ; // static variable static int b = 20 ; } class Base extends Parent { void rr() { // referring parent class(i.e, class Parent) // instance variable(i.e, a) System.out.println( super .a); // referring parent class(i.e, class Parent) // static variable(i.e, b) System.out.println( super .b); } public static void main(String[] args) { // Uncomment this and see here you get // Compile Time Error since cannot use 'super' // in static context. // super.a = 700; new Base().rr(); } } |
1020
这个和超级的相似之处
1) 我们可以使用 这 以及 超级的 除了静态区域以外的任何地方 。上面已经展示了这个例子,我们在公共静态void main(String[]args)中使用这个以及super,因此我们得到编译时错误,因为不能在静态区域中使用它们。 2) 我们可以使用 这 以及 超级的 程序中的任意次数 .
JAVA
// Java Program to illustrate using this // many number of times class RRR { // instance variable int a = 10 ; // static variable static int b = 20 ; void GFG() { // referring current class(i.e, class RR) // instance variable(i.e, a) this .a = 100 ; System.out.println(a); // referring current class(i.e, class RR) // static variable(i.e, b) this .b = 600 ; System.out.println(b); // referring current class(i.e, class RR) // instance variable(i.e, a) again this .a = 9000 ; System.out.println(a); } public static void main(String[] args) { new RRR().GFG(); } } |
1006009000
见上面我们用过的 这 三次。所以 这 可以多次使用。
JAVA
// Java Program to illustrate using super // many number of times class Parent { // instance variable int a = 36 ; // static variable static float x = 12 .2f; } class Base extends Parent { void GFG() { // referring super class(i.e, class Parent) // instance variable(i.e, a) super .a = 1 ; System.out.println(a); // referring super class(i.e, class Parent) // static variable(i.e, x) super .x = 60 .3f; System.out.println(x); } public static void main(String[] args) { new Base().GFG(); } } |
160.3
见上面我们用过的 超级的 两次。所以 超级的 可以多次使用。 注: 我们可以多次使用“this”和“super”,但最重要的是我们 不能 在静态上下文中使用它们。 让我们考虑一下 棘手的 此关键字的示例:
JAVA
// Java program to illustrate // the usage of this keyword class RR { int first = 22 ; int second = 33 ; void garcia( int a, int b) { a = this .first; b = this .second; System.out.println(first); System.out.println(second); System.out.println(a); System.out.println(b); } void louis( int m, int n) { this .first = m; this .second = n; System.out.println(first); System.out.println(second); System.out.println(m); System.out.println(n); } public static void main(String[] args) { new RR().garcia( 100 , 200 ); new RR().louis( 1 , 2 ); } } |
//it is of S.O.P(first) of garcia method22//it is of S.O.P(second) of garcia method33//it is of S.O.P(a) of garcia method22//it is of S.O.P(b) of garcia method33//it is of S.O.P(first) of louis method1//it is of S.O.P(second) of louis method2//it is of S.O.P(m) of louis method1//it is of S.O.P(n) of louis method2
项目流程: 首先,它从main开始,然后我们有 新RR()。加西亚(100200) 然后流向RR类的garcia方法,我们得到
a = this.firstb = this.second
这里发生的事情是,实例变量的值(即第一个)和静态变量的值(即第二个)分别分配给garcia方法的局部变量a和b。因此,a和b的值分别为22和33。接下来是 新RR()。路易(1,2) 因此,这里的流程是RR类的louis方法,我们有
this.first = mthis.second = n
在这里,上面发生的是路易斯方法的局部变量,即m和n的值被分别分配给实例和静态变量,即第一和第二。 因此,第一个和第二个变量的值与我们传递到louis方法的值相同,即分别为1和2。
我们能在同一个构造函数中同时使用this()和super()吗?
这里需要注意的一件有趣的事情是,根据Java指南,this()或super()必须是构造函数块中要执行的第一条语句。因此,我们不能将它们放在同一个构造函数中,因为它们都需要作为块中的第一条语句才能正确执行,这是不可能的。尝试这样做会引发编译器错误。
JAVA
class Vehicle { Vehicle() { System.out.println( "Vehicle is created." ); } } class Bike extends Vehicle { Bike() { System.out.println( "Bike is created." ); } Bike(String brand) { super (); // it calls Vehicle(), the parent class // constructor of class Bike this (); System.out.println( "Bike brand is " + brand); } } public class GFG { public static void main(String args[]) { Bike bike = new Bike( "Honda" ); } } |
在运行上述程序时,我们在第12行得到一个错误,即“调用this必须是构造函数中的第一个语句”。这是因为我们尝试同时使用super()和This()。虽然super()是构造函数的第一行,但这个()语句违反了它应该是第一行的条件,因此引发了一个错误。
编译错误:
prog.java:12: error: call to this must be first statement in constructor this(); ^1 error
本文由 拉贾特·拉瓦特 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。